File tree 4 files changed +91
-0
lines changed
4 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 34
34
"@nestjs/schematics" : " 6.7.0" ,
35
35
"@nestjs/testing" : " 6.8.5" ,
36
36
"@types/express" : " 4.17.2" ,
37
+ "@types/jest" : " ^24.0.18" ,
37
38
"@types/node" : " 12.12.5" ,
38
39
"@types/supertest" : " 2.0.8" ,
39
40
"jest" : " 24.9.0" ,
45
46
"tsconfig-paths" : " 3.9.0" ,
46
47
"tslint" : " 5.20.0" ,
47
48
"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
+ }
48
61
}
49
62
}
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments