Skip to content

Commit 2f1f2e8

Browse files
committed
refactor(MongoMemoryServer::cleanup): use "utils.removeDir" instead of custom
1 parent 26166e7 commit 2f1f2e8

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

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

+1-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import debug from 'debug';
1818
import { EventEmitter } from 'events';
1919
import { promises as fspromises } from 'fs';
2020
import { MongoClient } from 'mongodb';
21-
import { lt } from 'semver';
2221
import { EnsureInstanceError, StateError } from './util/errors';
2322
import * as os from 'os';
2423

@@ -611,13 +610,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
611610
} else {
612611
assertion(res.isDirectory(), new Error('Defined dbPath is not an directory'));
613612

614-
if (lt(process.version, '14.14.0')) {
615-
// this has to be used for 12.10 - 14.13 (inclusive) because ".rm" did not exist yet
616-
await fspromises.rmdir(dbPath, { recursive: true, maxRetries: 1 });
617-
} else {
618-
// this has to be used for 14.14+ (inclusive) because ".rmdir" and "recursive" got deprecated (DEP0147)
619-
await fspromises.rm(dbPath, { recursive: true, maxRetries: 1 });
620-
}
613+
await removeDir(dbPath);
621614
}
622615
}
623616

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import MongoMemoryServer, {
88
} from '../MongoMemoryServer';
99
import MongoInstance from '../util/MongoInstance';
1010
import * as utils from '../util/utils';
11-
import * as semver from 'semver';
1211
import { EnsureInstanceError, StateError } from '../util/errors';
1312
import { assertIsError } from './testUtils/test_utils';
1413
import { promises as fspromises } from 'fs';
@@ -773,8 +772,7 @@ describe('MongoMemoryServer', () => {
773772
// "beforeAll" dosnt work here, thanks to the top-level "afterAll" hook
774773
beforeEach(() => {
775774
jest.spyOn(utils, 'statPath');
776-
// @ts-expect-error because "default" dosnt exist in the definitions
777-
jest.spyOn(semver.default, 'lt'); // it needs to be ".default" otherwise "lt" is only an getter
775+
jest.spyOn(utils, 'removeDir');
778776
});
779777

780778
afterEach(async () => {
@@ -795,7 +793,7 @@ describe('MongoMemoryServer', () => {
795793
await mongoServer.cleanup();
796794

797795
expect(utils.statPath).not.toHaveBeenCalled();
798-
expect(semver.lt).not.toHaveBeenCalled();
796+
expect(utils.removeDir).toHaveBeenCalled();
799797
expect(await utils.statPath(dbPath)).toBeUndefined();
800798
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
801799
expect(mongoServer.instanceInfo).toBeUndefined();
@@ -811,7 +809,7 @@ describe('MongoMemoryServer', () => {
811809
await mongoServer.cleanup(true);
812810

813811
expect(utils.statPath).toHaveBeenCalledTimes(1);
814-
expect(semver.lt).not.toHaveBeenCalled();
812+
expect(utils.removeDir).toHaveBeenCalled();
815813
expect(await utils.statPath(dbPath)).toBeUndefined();
816814
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
817815
expect(mongoServer.instanceInfo).toBeUndefined();
@@ -828,7 +826,7 @@ describe('MongoMemoryServer', () => {
828826
await mongoServer.cleanup(true);
829827

830828
expect(utils.statPath).toHaveBeenCalledTimes(1);
831-
expect(semver.lt).toHaveBeenCalled(); // not testing on how many, because it would change with nodejs version
829+
expect(utils.removeDir).toHaveBeenCalled();
832830
expect(await utils.statPath(dbPath)).toBeUndefined();
833831
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
834832
expect(mongoServer.instanceInfo).toBeUndefined();
@@ -845,7 +843,7 @@ describe('MongoMemoryServer', () => {
845843
await mongoServer.stop({ doCleanup: false });
846844
await mongoServer.cleanup();
847845
expect(utils.statPath).not.toHaveBeenCalled();
848-
expect(semver.lt).not.toHaveBeenCalled();
846+
expect(utils.removeDir).toHaveBeenCalled();
849847
expect(await utils.statPath(dbPath)).toBeUndefined();
850848
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
851849
expect(mongoServer.instanceInfo).toBeUndefined();
@@ -863,7 +861,7 @@ describe('MongoMemoryServer', () => {
863861
await mongoServer.stop({ doCleanup: false });
864862
await mongoServer.cleanup({ doCleanup: true, force: true });
865863
expect(utils.statPath).toHaveBeenCalledTimes(1);
866-
expect(semver.lt).not.toHaveBeenCalled();
864+
expect(utils.removeDir).toHaveBeenCalled();
867865
expect(await utils.statPath(dbPath)).toBeUndefined();
868866
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
869867
expect(mongoServer.instanceInfo).toBeUndefined();
@@ -882,7 +880,7 @@ describe('MongoMemoryServer', () => {
882880
await mongoServer.stop({ doCleanup: false });
883881
await mongoServer.cleanup({ doCleanup: true, force: true });
884882
expect(utils.statPath).toHaveBeenCalledTimes(1);
885-
expect(semver.lt).toHaveBeenCalled(); // not testing on how many, because it would change with nodejs version
883+
expect(utils.removeDir).toHaveBeenCalled();
886884
expect(await utils.statPath(dbPath)).toBeUndefined();
887885
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
888886
expect(mongoServer.instanceInfo).toBeUndefined();

0 commit comments

Comments
 (0)