Skip to content

Commit faa8690

Browse files
committed
doc(samples) add unit tests for 13-mongo-typeorm
see nestjs#1539
1 parent 7914322 commit faa8690

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

sample/13-mongo-typeorm/package-lock.json

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/13-mongo-typeorm/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@nestjs/schematics": "6.7.0",
3535
"@nestjs/testing": "6.8.5",
3636
"@types/express": "4.17.2",
37+
"@types/jest": "^24.0.18",
3738
"@types/node": "12.12.5",
3839
"@types/supertest": "2.0.8",
3940
"jest": "24.9.0",
@@ -45,5 +46,17 @@
4546
"tsconfig-paths": "3.9.0",
4647
"tslint": "5.20.0",
4748
"typescript": "3.6.4"
49+
},
50+
"jest": {
51+
"moduleFileExtensions": [
52+
"js",
53+
"json",
54+
"ts"
55+
],
56+
"rootDir": "src",
57+
"testRegex": ".spec.ts$",
58+
"transform": {
59+
"^.+\\.(t|j)s$": "ts-jest"
60+
}
4861
}
4962
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Test } from '@nestjs/testing';
2+
import { PhotoController } from './photo.controller';
3+
import { PhotoService } from './photo.service';
4+
5+
describe('Photo Controller', () => {
6+
let controller: PhotoController;
7+
let service;
8+
9+
beforeAll(async () => {
10+
const module = await Test.createTestingModule({
11+
controllers: [PhotoController],
12+
providers: [
13+
{
14+
provide: PhotoService,
15+
useFactory: jest.fn(() => ({
16+
findAll: jest.fn(),
17+
})),
18+
},
19+
],
20+
}).compile();
21+
controller = module.get<PhotoController>(PhotoController);
22+
service = module.get(PhotoService);
23+
});
24+
25+
it('should return all photos from the service', async () => {
26+
service.findAll.mockReturnValueOnce([]);
27+
expect(await controller.findAll()).toEqual([]);
28+
expect(service.findAll).toHaveBeenCalledTimes(1);
29+
});
30+
31+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Test } from '@nestjs/testing';
2+
import { PhotoService } from './photo.service';
3+
import { getRepositoryToken } from '@nestjs/typeorm';
4+
import { Photo } from './photo.entity';
5+
6+
describe('Photo Service', () => {
7+
let service: PhotoService;
8+
let repository;
9+
10+
beforeAll(async () => {
11+
const module = await Test.createTestingModule({
12+
controllers: [PhotoService],
13+
providers: [
14+
{
15+
provide: getRepositoryToken(Photo),
16+
useFactory: jest.fn(() => ({
17+
find: jest.fn(),
18+
})),
19+
},
20+
],
21+
}).compile();
22+
service = module.get<PhotoService>(PhotoService);
23+
repository = module.get(getRepositoryToken(Photo));
24+
});
25+
26+
it('should return all photos from the repository', async () => {
27+
repository.find.mockReturnValueOnce([]);
28+
expect(await service.findAll()).toEqual([]);
29+
expect(repository.find).toHaveBeenCalledTimes(1);
30+
});
31+
32+
});

0 commit comments

Comments
 (0)