Skip to content

Commit

Permalink
test: Setup + utils tests (w/ Jest) (#5)
Browse files Browse the repository at this point in the history
* test: Jest setup

* test: Username

* chore: Test coverage

* fix: MongoDB in GitHub Actions

* fix: ENV config in GitHub Actions
  • Loading branch information
ChefKai authored Feb 15, 2021
1 parent fe54b4b commit f2dba08
Show file tree
Hide file tree
Showing 6 changed files with 3,035 additions and 67 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"es6": true,
"node": true
},
"plugins": [
"@typescript-eslint",
"jest"
],
"extends": [
"prettier",
"prettier/@typescript-eslint",
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Test

on:
workflow_dispatch:
pull_request:
push:
branches:
- develop
- master

jobs:
test:

name: Test
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 14.x

- name: Setup MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: 4.4

- name: Install dependencies
run: yarn install

- name: Run Jest
run: yarn test:coverage
env:
MONGO_URI: "mongodb://127.0.0.1:27017/jest"
71 changes: 71 additions & 0 deletions __tests__/valid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { isValid } from "../utils";
import { getConnection } from "../utils/mongo";
import { Connection } from "mongoose";

describe("UTILS - isValid", () => {
let connection: Connection | null = null;

beforeAll(async () => {
connection = await getConnection();
});

it("valid username", async () => {
const { valid, message } = await isValid("Jeff");

expect(valid).toStrictEqual(true);
expect(message).toStrictEqual(undefined);
});

it("cannot use a username of less than 3 characters", async () => {
const { valid, message } = await isValid("a");

expect(valid).toStrictEqual(false);
expect(message).toStrictEqual("Minimum length: 3 characters");
});

it("cannot use a username of more than 15 characters", async () => {
const { valid, message } = await isValid("aaaaaaaaaaaaaaaaaaaa");

expect(valid).toStrictEqual(false);
expect(message).toStrictEqual("Maximum length: 15 characters");
});

it("can only use alphanumeric characters", async () => {
const { valid, message } = await isValid("CakeUwU");

expect(valid).toStrictEqual(true);
expect(message).toStrictEqual(undefined);
});

it("can only use alphanumeric characters", async () => {
const { valid, message } = await isValid("C@keUwU");

expect(valid).toStrictEqual(false);
expect(message).toStrictEqual("No spaces or special characters");
});

it("cannot use a space in their username", async () => {
const { valid, message } = await isValid("aaaaa aaaaa");

expect(valid).toStrictEqual(false);
expect(message).toStrictEqual("No spaces or special characters");
});

it("cannot use a space (URI encoded) in their username", async () => {
const { valid, message } = await isValid("aaaaa%20aaaaa");

expect(valid).toStrictEqual(false);
expect(message).toStrictEqual("No spaces or special characters");
});

it("cannot create a username which violates blacklist", async () => {
const { valid, message } = await isValid("ChefCake");

expect(valid).toStrictEqual(false);
expect(message).toStrictEqual("Username not allowed");
});

afterAll(async () => {
await connection?.close();
});
});
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"license": "MIT",
"scripts": {
"format": "prettier --write '*/**/*.{js,ts}'",
"lint": "eslint '*/**/*.{js,ts}'"
"lint": "eslint '*/**/*.{js,ts}'",
"test": "jest",
"test:coverage": "jest --coverage"
},
"husky": {
"hooks": {
Expand All @@ -18,18 +20,22 @@
},
"dependencies": {
"ethereumjs-util": "^7.0.8",
"mongoose": "^5.11.15"
"mongoose": "^5.11.16"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@types/jest": "^26.0.20",
"@typescript-eslint/eslint-plugin": "^4.15.0",
"@typescript-eslint/parser": "^4.15.0",
"@vercel/node": "^1.9.0",
"eslint": "^7.19.0",
"eslint": "^7.20.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-jest": "^24.1.3",
"husky": "^4.3.8",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"ts-jest": "^26.5.1",
"typescript": "^4.1.5"
}
}
Loading

0 comments on commit f2dba08

Please sign in to comment.