Skip to content

Commit fc044b1

Browse files
authored
fix(tests) 90% coverage for unit testing (#64)
* test(all): add some unit tests * test(app): add some missing test files * test(app): 0failed and 0 error * test(app): add some tests for DTOs * test(app): hit 70% coverage * test(app): hit 80% coverage * test(app): hit 90% coverage & fix some lengths conditions
1 parent ddbd29e commit fc044b1

File tree

92 files changed

+8330
-2866
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+8330
-2866
lines changed

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,22 @@
122122
"^.+\\.(t|j)s$": "ts-jest"
123123
},
124124
"collectCoverageFrom": [
125-
"**/*.(t|j)s"
125+
"**/*.(t|j)s",
126+
"!**/*.dto.ts",
127+
"!**/*.entity.ts",
128+
"!**/*.interface.ts",
129+
"!**/*.enum.ts",
130+
"!**/*.constants.ts",
131+
"!**/*.swagger.ts",
132+
"!**/*.decorator.ts",
133+
"!**/index.ts",
134+
"!**/entities/**",
135+
"!**/enums/**",
136+
"!**/migrations/**",
137+
"!**/seeds/**",
138+
"!main.ts",
139+
"!**/*.spec.ts",
140+
"!**/node_modules/**"
126141
],
127142
"coverageDirectory": "../coverage",
128143
"testEnvironment": "node",

src/app.controller.spec.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,49 @@ import { AppController } from './app.controller';
33
import { AppService } from './app.service';
44

55
describe('AppController', () => {
6-
let appController: AppController;
6+
let app_controller: AppController;
7+
let app_service: AppService;
78

89
beforeEach(async () => {
910
const app: TestingModule = await Test.createTestingModule({
1011
controllers: [AppController],
1112
providers: [AppService],
1213
}).compile();
1314

14-
appController = app.get<AppController>(AppController);
15+
app_controller = app.get<AppController>(AppController);
16+
app_service = app.get<AppService>(AppService);
17+
});
18+
19+
afterEach(() => {
20+
jest.clearAllMocks();
21+
});
22+
23+
it('should be defined', () => {
24+
expect(app_controller).toBeDefined();
25+
expect(app_service).toBeDefined();
26+
});
27+
28+
describe('checkHealth', () => {
29+
it('should return application health status', () => {
30+
const result = app_controller.checkHealth();
31+
expect(result).toBe('Application is running');
32+
});
33+
34+
it('should call app_service.getHealthStatus', () => {
35+
const get_health_status_spy = jest.spyOn(app_service, 'getHealthStatus');
36+
37+
app_controller.checkHealth();
38+
39+
expect(get_health_status_spy).toHaveBeenCalledTimes(1);
40+
});
41+
42+
it('should return the exact string from service', () => {
43+
const mock_status = 'Custom health message';
44+
jest.spyOn(app_service, 'getHealthStatus').mockReturnValue(mock_status);
45+
46+
const result = app_controller.checkHealth();
47+
48+
expect(result).toBe(mock_status);
49+
});
1550
});
1651
});

src/app.module.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { AppModule } from './app.module';
2+
import { MiddlewareConsumer } from '@nestjs/common';
3+
4+
describe('AppModule', () => {
5+
it('should be defined', () => {
6+
expect(AppModule).toBeDefined();
7+
});
8+
9+
it('should be a class', () => {
10+
expect(typeof AppModule).toBe('function');
11+
});
12+
13+
it('should have module decorator metadata', () => {
14+
const imports = Reflect.getMetadata('imports', AppModule);
15+
expect(imports).toBeDefined();
16+
expect(Array.isArray(imports)).toBe(true);
17+
expect(imports.length).toBeGreaterThan(0);
18+
});
19+
20+
it('should have controllers metadata', () => {
21+
const controllers = Reflect.getMetadata('controllers', AppModule);
22+
expect(controllers).toBeDefined();
23+
expect(Array.isArray(controllers)).toBe(true);
24+
});
25+
26+
it('should have providers metadata', () => {
27+
const providers = Reflect.getMetadata('providers', AppModule);
28+
expect(providers).toBeDefined();
29+
expect(Array.isArray(providers)).toBe(true);
30+
});
31+
32+
describe('configure', () => {
33+
it('should have configure method for middleware', () => {
34+
const app_module = new AppModule();
35+
expect(app_module.configure).toBeDefined();
36+
expect(typeof app_module.configure).toBe('function');
37+
});
38+
39+
it('should call consumer.apply in configure method', () => {
40+
const app_module = new AppModule();
41+
const mock_consumer = {
42+
apply: jest.fn().mockReturnThis(),
43+
forRoutes: jest.fn().mockReturnThis(),
44+
} as unknown as MiddlewareConsumer;
45+
46+
app_module.configure(mock_consumer);
47+
48+
expect(mock_consumer.apply).toHaveBeenCalled();
49+
// expect(mock_consumer.forRoutes).toHaveBeenCalledWith('*');
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)