1
1
import { Test , TestingModule } from '@nestjs/testing' ;
2
2
import { INestApplication , ValidationPipe } from '@nestjs/common' ;
3
- import request from 'supertest' ;
4
- import { AppModule } from '../src/app.module' ;
3
+ import * as request from 'supertest' ;
4
+ import { AppModule } from 'src/app.module' ;
5
+ import { User } from 'src/users/entities/user.entity' ;
5
6
6
7
describe ( 'UsersModule (e2e)' , ( ) => {
7
8
let app : INestApplication ;
@@ -26,27 +27,33 @@ describe('UsersModule (e2e)', () => {
26
27
email : 'simone@example.com' ,
27
28
} ;
28
29
29
- const res = await request ( app . getHttpServer ( ) )
30
+ const server = app . getHttpServer ( ) as unknown as Parameters < typeof request > [ 0 ] ;
31
+
32
+ const res = await request ( server )
30
33
. post ( '/api/v1/users' )
31
34
. send ( userDto )
32
35
. expect ( 201 ) ;
33
36
34
- expect ( res . body ) . toEqual ( {
37
+ const user = res . body as User ;
38
+ expect ( user ) . toEqual ( {
35
39
id : expect . any ( Number ) ,
36
40
name : userDto . name ,
37
41
email : userDto . email ,
38
42
} ) ;
39
43
} ) ;
40
44
41
45
it ( '/api/v1/users (GET) should return all users' , async ( ) => {
42
- const res = await request ( app . getHttpServer ( ) )
46
+ const server = app . getHttpServer ( ) as unknown as Parameters < typeof request > [ 0 ] ;
47
+
48
+ const res = await request ( server )
43
49
. get ( '/api/v1/users' )
44
50
. expect ( 200 ) ;
45
51
46
- expect ( Array . isArray ( res . body ) ) . toBe ( true ) ;
47
- expect ( res . body [ 0 ] ) . toHaveProperty ( 'id' ) ;
48
- expect ( res . body [ 0 ] ) . toHaveProperty ( 'name' ) ;
49
- expect ( res . body [ 0 ] ) . toHaveProperty ( 'email' ) ;
52
+ const users = res . body as User [ ] ;
53
+ expect ( Array . isArray ( users ) ) . toBe ( true ) ;
54
+ expect ( users [ 0 ] ) . toHaveProperty ( 'id' ) ;
55
+ expect ( users [ 0 ] ) . toHaveProperty ( 'name' ) ;
56
+ expect ( users [ 0 ] ) . toHaveProperty ( 'email' ) ;
50
57
} ) ;
51
58
52
59
afterAll ( async ( ) => {
0 commit comments