Skip to content

Commit 1acb1b2

Browse files
author
hersveit
committed
api unit test base
1 parent 8cca21f commit 1acb1b2

File tree

9 files changed

+117
-8
lines changed

9 files changed

+117
-8
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ConfigModel } from '../config/config.model';
2+
3+
export class ConfigServiceMock implements ConfigModel {
4+
DB_NAME: string;
5+
DB_HOST: string;
6+
DB_USERNAME: string;
7+
DB_PASSWORD: string;
8+
DB_PORT: number;
9+
PORT: number;
10+
JWT_SECRET: string;
11+
JWT_EXPIRES_IN: string;
12+
JWT_REFRESH_SECRET: string;
13+
JWT_REFRESH_EXPIRES_IN: string;
14+
DOMAIN: string;
15+
16+
constructor() {
17+
this.DB_NAME = 'db';
18+
this.DB_HOST = 'localhost';
19+
this.DB_USERNAME = 'admin';
20+
this.DB_PASSWORD = 'pass';
21+
this.DB_PORT = 5432;
22+
this.PORT = 3000;
23+
this.JWT_SECRET = 'jwtSecret';
24+
this.JWT_EXPIRES_IN = '10min';
25+
this.JWT_REFRESH_SECRET = 'jwtRefreshSecret';
26+
this.JWT_REFRESH_EXPIRES_IN = '2days';
27+
this.DOMAIN = `http://localhost:${this.PORT}`;
28+
}
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { DeepMocked, createMock } from '@golevelup/ts-jest';
2+
import { RepositoryService } from '@lib/repository';
3+
import { UserEntity } from '@lib/repository/entities/user.entity';
4+
import { Repository } from 'typeorm';
5+
6+
export const getRepositoryServiceMock = () => {
7+
return {
8+
user: createMock<Repository<UserEntity>>(),
9+
} as unknown as DeepMocked<RepositoryService>;
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { UserEntity } from '@lib/repository/entities/user.entity';
2+
3+
export const getUserStub = (): UserEntity => ({
4+
id: 0,
5+
email: 'email@example.com',
6+
createdAt: new Date(),
7+
hash: '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8', // password
8+
emailConfirmed: false,
9+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { createMock } from '@golevelup/ts-jest';
2+
import { CoreConfigService } from '@lib/core';
3+
import { RepositoryService } from '@lib/repository';
4+
import { JwtService } from '@nestjs/jwt';
5+
import { Test } from '@nestjs/testing';
6+
import { ConfigServiceMock } from '../../../__mocks__/ConfigServiceMock';
7+
import { getRepositoryServiceMock } from '../../../__mocks__/RepositoryServiceMock';
8+
import { getUserStub } from '../../../__mocks__/stubs/user.stub';
9+
import { ConfigModel } from '../../../config/config.model';
10+
import { UserService } from '../../user/user.service';
11+
import { AuthService } from '../auth.service';
12+
13+
describe('AuthService', () => {
14+
let rep: ReturnType<typeof getRepositoryServiceMock>;
15+
let user: ReturnType<typeof getUserStub>;
16+
let authService: AuthService;
17+
let configService: CoreConfigService<ConfigModel>;
18+
let userService: UserService;
19+
20+
beforeEach(async () => {
21+
const module = await Test.createTestingModule({
22+
providers: [
23+
AuthService,
24+
JwtService,
25+
{ provide: CoreConfigService, useClass: ConfigServiceMock },
26+
{
27+
provide: RepositoryService,
28+
useValue: getRepositoryServiceMock(),
29+
},
30+
{
31+
provide: UserService,
32+
useValue: createMock<UserService>(),
33+
},
34+
],
35+
}).compile();
36+
37+
authService = module.get(AuthService);
38+
configService = module.get(CoreConfigService);
39+
rep = module.get(RepositoryService);
40+
userService = module.get(UserService);
41+
42+
user = getUserStub();
43+
});
44+
45+
it('should be defined service and all deps', () => {
46+
expect(authService).toBeDefined();
47+
expect(configService).toBeDefined();
48+
expect(rep).toBeDefined();
49+
expect(userService).toBeDefined();
50+
expect(user).toBeDefined();
51+
});
52+
});

apps/api/tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"outDir": "../../dist/apps/api"
66
},
77
"include": ["src/**/*"],
8-
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
8+
"exclude": ["node_modules", "dist"]
99
}

libs/core/tsconfig.lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"outDir": "../../dist/libs/core"
66
},
77
"include": ["src/**/*"],
8-
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
8+
"exclude": ["node_modules", "dist"]
99
}

libs/repository/tsconfig.lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"outDir": "../../dist/libs/repository"
66
},
77
"include": ["src/**/*"],
8-
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
8+
"exclude": ["node_modules", "dist"]
99
}

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
"typeorm:run": "node ./scripts/typeorm/migrationRun.js",
1414
"api:build": "nest build api",
1515
"api:dev": "NODE_ENV=development APP=api nest start api --watch",
16-
"api:prod": "NODE_ENV=production APP=api node dist/apps/api/apps/api/src/main.js"
16+
"api:prod": "NODE_ENV=production APP=api node dist/apps/api/apps/api/src/main.js",
17+
"test": "jest"
1718
},
1819
"dependencies": {
20+
"@golevelup/ts-jest": "^0.5.0",
1921
"@nestjs-cls/transactional": "^2.3.1",
2022
"@nestjs-cls/transactional-adapter-typeorm": "^1.2.1",
2123
"@nestjs/common": "^10.0.0",
@@ -42,7 +44,7 @@
4244
"devDependencies": {
4345
"@nestjs/cli": "^10.0.0",
4446
"@nestjs/schematics": "^10.0.0",
45-
"@nestjs/testing": "^10.0.0",
47+
"@nestjs/testing": "^10.3.8",
4648
"@types/cookie-parser": "^1.4.7",
4749
"@types/express": "^4.17.17",
4850
"@types/jest": "^29.5.2",
@@ -89,8 +91,8 @@
8991
"<rootDir>/apps/"
9092
],
9193
"moduleNameMapper": {
92-
"^@starter-kit/repository(|/.*)$": "<rootDir>/libs/repository/src/$1",
93-
"^@starter-kit/core(|/.*)$": "<rootDir>/libs/core/src/$1"
94+
"^@lib/repository(|/.*)$": "<rootDir>/libs/repository/src/$1",
95+
"^@lib/core(|/.*)$": "<rootDir>/libs/core/src/$1"
9496
}
9597
}
9698
}

pnpm-lock.yaml

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)