|
| 1 | +import MongoMemoryServer from '../MongoMemoryServer'; |
| 2 | + |
| 3 | +jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000; |
| 4 | + |
| 5 | +describe('MongoMemoryServer', () => { |
| 6 | + describe('start', () => { |
| 7 | + const mockStartUpInstance = jest.fn(); |
| 8 | + |
| 9 | + afterEach(() => { |
| 10 | + mockStartUpInstance.mockClear(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should resolve to true if an MongoInstanceData is resolved by _startUpInstance', async () => { |
| 14 | + mockStartUpInstance.mockResolvedValue({}); |
| 15 | + MongoMemoryServer.prototype._startUpInstance = mockStartUpInstance; |
| 16 | + |
| 17 | + const mongoServer = new MongoMemoryServer({ autoStart: false }); |
| 18 | + |
| 19 | + expect(mockStartUpInstance).toHaveBeenCalledTimes(0); |
| 20 | + |
| 21 | + await expect(mongoServer.start()).resolves.toEqual(true); |
| 22 | + |
| 23 | + expect(mockStartUpInstance).toHaveBeenCalledTimes(1); |
| 24 | + }); |
| 25 | + |
| 26 | + 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 |
| 28 | + .mockRejectedValueOnce(new Error('Mongod shutting down')) |
| 29 | + .mockResolvedValueOnce({}); |
| 30 | + MongoMemoryServer.prototype._startUpInstance = mockStartUpInstance; |
| 31 | + |
| 32 | + const mongoServer = new MongoMemoryServer({ |
| 33 | + autoStart: false, |
| 34 | + instance: { |
| 35 | + port: 123, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(mockStartUpInstance).toHaveBeenCalledTimes(0); |
| 40 | + |
| 41 | + await expect(mongoServer.start()).resolves.toEqual(true); |
| 42 | + |
| 43 | + expect(mockStartUpInstance).toHaveBeenCalledTimes(2); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should throw an error if _startUpInstance throws an unknown error', async () => { |
| 47 | + mockStartUpInstance.mockRejectedValueOnce(new Error('unknown error')); |
| 48 | + MongoMemoryServer.prototype._startUpInstance = mockStartUpInstance; |
| 49 | + |
| 50 | + const mongoServer = new MongoMemoryServer({ |
| 51 | + autoStart: false, |
| 52 | + instance: { |
| 53 | + port: 123, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + expect(mockStartUpInstance).toHaveBeenCalledTimes(0); |
| 58 | + |
| 59 | + await expect(mongoServer.start()).rejects.toThrow( |
| 60 | + `unknown error\n\nUse debug option for more info: ` + |
| 61 | + `new MongoMemoryServer({ debug: true })` |
| 62 | + ); |
| 63 | + |
| 64 | + expect(mockStartUpInstance).toHaveBeenCalledTimes(1); |
| 65 | + }); |
| 66 | + }); |
| 67 | + describe('getInstanceData', () => { |
| 68 | + const mockStart = jest.fn(); |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + mockStart.mockClear(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should throw an error if not instance is running after calling start', async () => { |
| 75 | + mockStart.mockResolvedValue(true); |
| 76 | + MongoMemoryServer.prototype.start = mockStart; |
| 77 | + |
| 78 | + const mongoServer = new MongoMemoryServer({ autoStart: false }); |
| 79 | + |
| 80 | + await expect(mongoServer.getInstanceData()).rejects.toThrow( |
| 81 | + '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.' |
| 82 | + ); |
| 83 | + |
| 84 | + expect(mockStart).toHaveBeenCalledTimes(1); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments