Skip to content

Commit ead7958

Browse files
committed
test(MongoMemoryServer): better cleanup created temporary directories
1 parent 1a8ac8a commit ead7958

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -902,22 +902,22 @@ describe('MongoMemoryServer', () => {
902902

903903
describe('getStartOptions()', () => {
904904
it('should create a tmpdir if "dbPath" is not set', async () => {
905-
// somehow, jest cannot redefine function "dirSync", so this is disabled
906-
// const tmpSpy = jest.spyOn(tmp, 'dirSync');
905+
const tmpSpy = jest.spyOn(utils, 'createTmpDir');
907906
const mongoServer = new MongoMemoryServer({});
908907

909908
// @ts-expect-error "getStartOptions" is protected
910909
const options = await mongoServer.getStartOptions();
911910

912-
// see comment above
913-
// expect(tmpSpy).toHaveBeenCalledTimes(1);
911+
expect(tmpSpy).toHaveBeenCalledTimes(1);
914912
expect(options.data.tmpDir).toBeDefined();
915913
// jest "expect" do not act as typescript typeguards
916914
utils.assertion(
917915
!utils.isNullOrUndefined(options.data.dbPath),
918916
new Error('Expected "options.data.dbPath" to be defined')
919917
);
920918
expect(await utils.pathExists(options.data.dbPath)).toEqual(true);
919+
920+
await utils.removeDir(options.data.tmpDir!); // manual cleanup
921921
});
922922

923923
it('should resolve "isNew" to "true" and set "createAuth" to "true" when dbPath is set, but empty', async () => {
@@ -986,6 +986,8 @@ describe('MongoMemoryServer', () => {
986986

987987
expect(newPortSpy).toHaveBeenCalledTimes(1);
988988
expect(typeof options.data.port === 'number').toBeTruthy();
989+
990+
await utils.removeDir(options.data.tmpDir!); // manual cleanup
989991
});
990992

991993
it('should use a predefined port as a suggestion for a port', async () => {
@@ -1003,6 +1005,8 @@ describe('MongoMemoryServer', () => {
10031005

10041006
expect(newPortSpy).toHaveBeenCalledWith(predefinedPort);
10051007
expect(options.data.port).toStrictEqual(predefinedPort);
1008+
1009+
await utils.removeDir(options.data.tmpDir!); // manual cleanup
10061010
});
10071011

10081012
it('should use a predefined port for a port with "forceSamePort" on', async () => {
@@ -1019,6 +1023,8 @@ describe('MongoMemoryServer', () => {
10191023

10201024
expect(newPortSpy).not.toHaveBeenCalled();
10211025
expect(options.data.port).toStrictEqual(predefinedPort);
1026+
1027+
await utils.removeDir(options.data.tmpDir!); // manual cleanup
10221028
});
10231029
});
10241030

@@ -1126,5 +1132,10 @@ describe('MongoMemoryServer', () => {
11261132

11271133
expect(createSpy.mock.calls.length).toStrictEqual(1);
11281134
expect(createSpy.mock.calls[0][0].instance).toHaveProperty('launchTimeout', 2000);
1135+
1136+
await utils.removeDir(
1137+
// @ts-expect-error "_instanceInfo" is protected
1138+
mongoServer._instanceInfo.tmpDir!
1139+
); // manual cleanup
11291140
});
11301141
});

0 commit comments

Comments
 (0)