File tree 2 files changed +31
-3
lines changed
2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { IndexController } from '../../src/controllers/IndexController' ;
2
- import { Request , Response } from 'express' ;
2
+ import { NextFunction , Request , Response } from 'express' ;
3
3
4
4
describe ( 'IndexController' , ( ) => {
5
5
it ( 'should respond as expected' , ( ) => {
6
6
const request = { } ;
7
7
const response = {
8
8
json : jest . fn ( ) ,
9
9
} ;
10
+ const next : NextFunction = jest . fn ( ) ;
10
11
11
- IndexController ( request as Request , response as unknown as Response ) ;
12
+ IndexController ( request as Request , response as unknown as Response , next ) ;
12
13
13
- expect ( response . json ) . toBeCalledWith ( [ 'ok' ] ) ;
14
+ expect ( response . json ) . toBeCalledWith (
15
+ expect . objectContaining ( {
16
+ status : 200 ,
17
+ } ) ,
18
+ ) ;
14
19
} ) ;
15
20
} ) ;
Original file line number Diff line number Diff line change 1
1
import { AppDataSource , initialiseDataSource , destroyDataSource } from '../../src/database' ;
2
2
3
3
describe ( 'DataSource Utility' , ( ) => {
4
+ let initCount : number = 0 ;
4
5
const originalConsoleError = console . error ;
5
6
6
7
beforeEach ( async ( ) => {
7
8
console . error = jest . fn ( ) ;
8
9
10
+ jest . spyOn ( AppDataSource , 'initialize' ) . mockImplementation ( async ( ) => {
11
+ initCount ++ ;
12
+ if ( initCount > 1 ) {
13
+ throw new Error ( 'initialize called more than once before destroying.' ) ;
14
+ }
15
+ Object . defineProperty ( AppDataSource , 'isInitialized' , {
16
+ value : true ,
17
+ writable : true ,
18
+ configurable : true ,
19
+ } ) ;
20
+ return AppDataSource ;
21
+ } ) ;
22
+
23
+ jest . spyOn ( AppDataSource , 'destroy' ) . mockImplementation ( async ( ) => {
24
+ initCount = 0 ;
25
+ Object . defineProperty ( AppDataSource , 'isInitialized' , {
26
+ value : false ,
27
+ writable : true ,
28
+ configurable : true ,
29
+ } ) ;
30
+ } ) ;
31
+
9
32
if ( ! AppDataSource . isInitialized ) {
10
33
await initialiseDataSource ( ) ;
11
34
}
You can’t perform that action at this time.
0 commit comments