1- import MongoMemoryServer from '../MongoMemoryServer' ;
1+ import MongoMemoryServerType from '../MongoMemoryServer' ;
22
33jasmine . DEFAULT_TIMEOUT_INTERVAL = 600000 ;
44
55describe ( 'MongoMemoryServer' , ( ) => {
6- describe ( 'start' , ( ) => {
7- const mockStartUpInstance = jest . fn ( ) ;
8-
9- afterEach ( ( ) => {
10- mockStartUpInstance . mockClear ( ) ;
11- } ) ;
6+ let MongoMemoryServer : typeof MongoMemoryServerType ;
7+ beforeEach ( ( ) => {
8+ jest . resetModules ( ) ;
9+ MongoMemoryServer = jest . requireActual ( '../MongoMemoryServer' ) . default ;
10+ } ) ;
1211
12+ describe ( 'start' , ( ) => {
1313 it ( 'should resolve to true if an MongoInstanceData is resolved by _startUpInstance' , async ( ) => {
14- mockStartUpInstance . mockResolvedValue ( { } ) ;
15- MongoMemoryServer . prototype . _startUpInstance = mockStartUpInstance ;
14+ MongoMemoryServer . prototype . _startUpInstance = jest . fn ( ( ) => Promise . resolve ( { } as any ) ) ;
1615
1716 const mongoServer = new MongoMemoryServer ( { autoStart : false } ) ;
1817
19- expect ( mockStartUpInstance ) . toHaveBeenCalledTimes ( 0 ) ;
18+ expect ( MongoMemoryServer . prototype . _startUpInstance ) . toHaveBeenCalledTimes ( 0 ) ;
2019
2120 await expect ( mongoServer . start ( ) ) . resolves . toEqual ( true ) ;
2221
23- expect ( mockStartUpInstance ) . toHaveBeenCalledTimes ( 1 ) ;
22+ expect ( MongoMemoryServer . prototype . _startUpInstance ) . toHaveBeenCalledTimes ( 1 ) ;
2423 } ) ;
2524
2625 it ( '_startUpInstance should be called a second time if an error is thrown on the first call and assign the current port to nulll' , async ( ) => {
27- mockStartUpInstance
26+ MongoMemoryServer . prototype . _startUpInstance = jest
27+ . fn ( )
2828 . mockRejectedValueOnce ( new Error ( 'Mongod shutting down' ) )
2929 . mockResolvedValueOnce ( { } ) ;
30- MongoMemoryServer . prototype . _startUpInstance = mockStartUpInstance ;
3130
3231 const mongoServer = new MongoMemoryServer ( {
3332 autoStart : false ,
@@ -36,16 +35,17 @@ describe('MongoMemoryServer', () => {
3635 } ,
3736 } ) ;
3837
39- expect ( mockStartUpInstance ) . toHaveBeenCalledTimes ( 0 ) ;
38+ expect ( MongoMemoryServer . prototype . _startUpInstance ) . toHaveBeenCalledTimes ( 0 ) ;
4039
4140 await expect ( mongoServer . start ( ) ) . resolves . toEqual ( true ) ;
4241
43- expect ( mockStartUpInstance ) . toHaveBeenCalledTimes ( 2 ) ;
42+ expect ( MongoMemoryServer . prototype . _startUpInstance ) . toHaveBeenCalledTimes ( 2 ) ;
4443 } ) ;
4544
4645 it ( 'should throw an error if _startUpInstance throws an unknown error' , async ( ) => {
47- mockStartUpInstance . mockRejectedValueOnce ( new Error ( 'unknown error' ) ) ;
48- MongoMemoryServer . prototype . _startUpInstance = mockStartUpInstance ;
46+ MongoMemoryServer . prototype . _startUpInstance = jest
47+ . fn ( )
48+ . mockRejectedValueOnce ( new Error ( 'unknown error' ) ) ;
4949
5050 const mongoServer = new MongoMemoryServer ( {
5151 autoStart : false ,
@@ -54,34 +54,43 @@ describe('MongoMemoryServer', () => {
5454 } ,
5555 } ) ;
5656
57- expect ( mockStartUpInstance ) . toHaveBeenCalledTimes ( 0 ) ;
57+ expect ( MongoMemoryServer . prototype . _startUpInstance ) . toHaveBeenCalledTimes ( 0 ) ;
5858
5959 await expect ( mongoServer . start ( ) ) . rejects . toThrow (
6060 `unknown error\n\nUse debug option for more info: ` +
6161 `new MongoMemoryServer({ debug: true })`
6262 ) ;
6363
64- expect ( mockStartUpInstance ) . toHaveBeenCalledTimes ( 1 ) ;
64+ expect ( MongoMemoryServer . prototype . _startUpInstance ) . toHaveBeenCalledTimes ( 1 ) ;
6565 } ) ;
6666 } ) ;
67- describe ( 'getInstanceData' , ( ) => {
68- const mockStart = jest . fn ( ) ;
69-
70- afterEach ( ( ) => {
71- mockStart . mockClear ( ) ;
72- } ) ;
7367
68+ describe ( 'getInstanceData' , ( ) => {
7469 it ( 'should throw an error if not instance is running after calling start' , async ( ) => {
75- mockStart . mockResolvedValue ( true ) ;
76- MongoMemoryServer . prototype . start = mockStart ;
70+ MongoMemoryServer . prototype . start = jest . fn ( ( ) => Promise . resolve ( true ) ) ;
7771
7872 const mongoServer = new MongoMemoryServer ( { autoStart : false } ) ;
7973
8074 await expect ( mongoServer . getInstanceData ( ) ) . rejects . toThrow (
8175 'Database instance is not running. You should start database by calling start() method. BTW it should start automatically if opts.autoStart!=false. Also you may provide opts.debug=true for more info.'
8276 ) ;
8377
84- expect ( mockStart ) . toHaveBeenCalledTimes ( 1 ) ;
78+ expect ( MongoMemoryServer . prototype . start ) . toHaveBeenCalledTimes ( 1 ) ;
79+ } ) ;
80+ } ) ;
81+
82+ describe ( 'stop' , ( ) => {
83+ it . only ( 'should stop mongod' , async ( ) => {
84+ console . log ( MongoMemoryServer . prototype . _startUpInstance ) ;
85+ const mongod = new MongoMemoryServer ( {
86+ autoStart : true ,
87+ debug : true ,
88+ } ) ;
89+
90+ await mongod . getInstanceData ( ) ;
91+
92+ await mongod . stop ( ) ;
93+ expect ( mongod . runningInstance ) . toBe ( null ) ;
8594 } ) ;
8695 } ) ;
8796} ) ;
0 commit comments