Skip to content

Commit c72533b

Browse files
author
hersveit
authored
Merge pull request #7 from fullstack-development/3-tests
Add tests
2 parents d40a1de + f694ee6 commit c72533b

File tree

20 files changed

+1594
-56
lines changed

20 files changed

+1594
-56
lines changed

package.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"start:debug": "NODE_ENV=development nest start --debug --watch",
1414
"start:prod": "NODE_ENV=production node dist/main",
1515
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
16+
"test": "jest",
17+
"test:coverage": "jest --coverage",
1618
"migrations:create": "npx typeorm migration:create -o -d migrations -n",
1719
"migrations:run:dev": "node migrations.js run --dev",
1820
"migrations:revert:dev": "node migrations.js revert --dev",
@@ -39,6 +41,7 @@
3941
"date-fns": "^2.21.3",
4042
"dotenv": "^8.2.0",
4143
"eslint-plugin-nestjs-kit": "link:./eslint-plugin",
44+
"express": "^4.17.2",
4245
"iterate": "^0.1.1",
4346
"monocle-ts": "^2.3.9",
4447
"mysql2": "^2.2.5",
@@ -57,9 +60,10 @@
5760
"yamljs": "^0.3.0"
5861
},
5962
"devDependencies": {
63+
"@golevelup/ts-jest": "^0.3.2",
6064
"@nestjs/cli": "^7.5.1",
6165
"@nestjs/schematics": "^7.1.3",
62-
"@nestjs/testing": "^7.5.1",
66+
"@nestjs/testing": "^7.6.5",
6367
"@types/cookie-parser": "^1.4.2",
6468
"@types/express": "^4.17.8",
6569
"@types/jest": "^26.0.15",
@@ -80,7 +84,7 @@
8084
"eslint-plugin-prettier": "^3.1.4",
8185
"jest": "^26.6.3",
8286
"prettier": "^2.1.2",
83-
"supertest": "^6.0.0",
87+
"supertest": "^6.2.1",
8488
"ts-jest": "^26.4.3",
8589
"ts-loader": "^8.0.8",
8690
"ts-mockito": "^2.6.1",
@@ -94,15 +98,24 @@
9498
"json",
9599
"ts"
96100
],
97-
"rootDir": "src",
101+
"moduleDirectories": [
102+
"<rootDir>/node_modules",
103+
"<rootDir>/src"
104+
],
105+
"modulePathIgnorePatterns": [
106+
"<rootDir>/dist/",
107+
"<rootDir>/eslint-plugin/",
108+
"<rootDir>/migrations/",
109+
"<rootDir>/coverage/"
110+
],
98111
"testRegex": ".*\\.spec\\.ts$",
99112
"transform": {
100113
"^.+\\.(t|j)s$": "ts-jest"
101114
},
102115
"collectCoverageFrom": [
103116
"**/*.(t|j)s"
104117
],
105-
"coverageDirectory": "../coverage",
118+
"coverageDirectory": "coverage",
106119
"testEnvironment": "node"
107120
}
108121
}

src/__mocks__/ConfigServiceFake.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export class ConfigServiceFake {
2+
IS_DEV: boolean;
3+
IS_TEST: boolean;
4+
DB_ADDRESS: string;
5+
DB_USER: string;
6+
DB_PASSWORD: string;
7+
DB_NAME: string;
8+
DB_PORT: number;
9+
JWT_SECRET: string;
10+
JWT_EXPIRES_IN: string;
11+
JWT_REFRESH_TOKEN_EXPIRATION_TIME: string;
12+
JWT_REFRESH_TOKEN_SECRET: string;
13+
PORT_API: number;
14+
DOMAIN: string;
15+
constructor() {
16+
this.IS_DEV = true;
17+
this.IS_TEST = true;
18+
this.DB_ADDRESS = 'fake url';
19+
this.DB_USER = 'user';
20+
this.DB_PASSWORD = 'password';
21+
this.DB_NAME = 'dbName';
22+
this.DB_PORT = 8080;
23+
this.JWT_SECRET = 'jwtSecret';
24+
this.JWT_EXPIRES_IN = '10 min';
25+
this.JWT_REFRESH_TOKEN_EXPIRATION_TIME = '2 days';
26+
this.JWT_REFRESH_TOKEN_SECRET = 'jwtRefreshSecret';
27+
this.PORT_API = 3000;
28+
this.DOMAIN = `http://localhost:${this.PORT_API}`;
29+
}
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { DeepMocked, createMock } from '@golevelup/ts-jest';
2+
import { RequestContext } from '@medibloc/nestjs-request-context';
3+
import { Transactions } from '../utils/transactions.utils';
4+
5+
export class TransactionsContextFake extends RequestContext {
6+
transactions: DeepMocked<Transactions>;
7+
8+
constructor() {
9+
super();
10+
this.transactions = createMock<Transactions>();
11+
}
12+
13+
getTransactions() {
14+
return this.transactions;
15+
}
16+
}

src/__mocks__/confirmEmail.stub.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { EmailConfirmEntity } from '../repositories/emailConfirms/emailConfirm.entity';
2+
3+
export const getConfirmEmailStub = (): EmailConfirmEntity => {
4+
return {
5+
id: 0,
6+
confirmId: 'test uuid',
7+
userId: 0,
8+
};
9+
};

src/__mocks__/error.stub.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { InsertResult } from 'typeorm';
2+
import { ErrorEntity } from '../repositories/errors/errors.entity';
3+
4+
export const getErrorStub = (): ErrorEntity => {
5+
return {
6+
uuid: 'test uuid',
7+
id: 0,
8+
error: 'test error',
9+
};
10+
};
11+
12+
export const getInsertResult = (): InsertResult => {
13+
return { raw: [{ id: 0 }] } as InsertResult;
14+
};

src/__mocks__/user.stub.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { UserEntity } from '../repositories/users/user.entity';
2+
3+
export const getUserStub = (): UserEntity => {
4+
return {
5+
id: 0,
6+
email: 'test@example.com',
7+
hash: 'test hash',
8+
emailConfirmed: false,
9+
created: new Date().toISOString() as unknown as Date,
10+
refreshToken: null,
11+
};
12+
};

0 commit comments

Comments
 (0)