Skip to content

Commit

Permalink
refactor(MongoMemoryServer::cleanup): use "utils.removeDir" instead o…
Browse files Browse the repository at this point in the history
…f custom
  • Loading branch information
hasezoey committed Mar 8, 2023
1 parent 26166e7 commit 2f1f2e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
9 changes: 1 addition & 8 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import debug from 'debug';
import { EventEmitter } from 'events';
import { promises as fspromises } from 'fs';
import { MongoClient } from 'mongodb';
import { lt } from 'semver';
import { EnsureInstanceError, StateError } from './util/errors';
import * as os from 'os';

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import MongoMemoryServer, {
} from '../MongoMemoryServer';
import MongoInstance from '../util/MongoInstance';
import * as utils from '../util/utils';
import * as semver from 'semver';
import { EnsureInstanceError, StateError } from '../util/errors';
import { assertIsError } from './testUtils/test_utils';
import { promises as fspromises } from 'fs';
Expand Down Expand Up @@ -773,8 +772,7 @@ describe('MongoMemoryServer', () => {
// "beforeAll" dosnt work here, thanks to the top-level "afterAll" hook
beforeEach(() => {
jest.spyOn(utils, 'statPath');
// @ts-expect-error because "default" dosnt exist in the definitions
jest.spyOn(semver.default, 'lt'); // it needs to be ".default" otherwise "lt" is only an getter
jest.spyOn(utils, 'removeDir');
});

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

expect(utils.statPath).not.toHaveBeenCalled();
expect(semver.lt).not.toHaveBeenCalled();
expect(utils.removeDir).toHaveBeenCalled();
expect(await utils.statPath(dbPath)).toBeUndefined();
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
expect(mongoServer.instanceInfo).toBeUndefined();
Expand All @@ -811,7 +809,7 @@ describe('MongoMemoryServer', () => {
await mongoServer.cleanup(true);

expect(utils.statPath).toHaveBeenCalledTimes(1);
expect(semver.lt).not.toHaveBeenCalled();
expect(utils.removeDir).toHaveBeenCalled();
expect(await utils.statPath(dbPath)).toBeUndefined();
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
expect(mongoServer.instanceInfo).toBeUndefined();
Expand All @@ -828,7 +826,7 @@ describe('MongoMemoryServer', () => {
await mongoServer.cleanup(true);

expect(utils.statPath).toHaveBeenCalledTimes(1);
expect(semver.lt).toHaveBeenCalled(); // not testing on how many, because it would change with nodejs version
expect(utils.removeDir).toHaveBeenCalled();
expect(await utils.statPath(dbPath)).toBeUndefined();
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
expect(mongoServer.instanceInfo).toBeUndefined();
Expand All @@ -845,7 +843,7 @@ describe('MongoMemoryServer', () => {
await mongoServer.stop({ doCleanup: false });
await mongoServer.cleanup();
expect(utils.statPath).not.toHaveBeenCalled();
expect(semver.lt).not.toHaveBeenCalled();
expect(utils.removeDir).toHaveBeenCalled();
expect(await utils.statPath(dbPath)).toBeUndefined();
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
expect(mongoServer.instanceInfo).toBeUndefined();
Expand All @@ -863,7 +861,7 @@ describe('MongoMemoryServer', () => {
await mongoServer.stop({ doCleanup: false });
await mongoServer.cleanup({ doCleanup: true, force: true });
expect(utils.statPath).toHaveBeenCalledTimes(1);
expect(semver.lt).not.toHaveBeenCalled();
expect(utils.removeDir).toHaveBeenCalled();
expect(await utils.statPath(dbPath)).toBeUndefined();
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
expect(mongoServer.instanceInfo).toBeUndefined();
Expand All @@ -882,7 +880,7 @@ describe('MongoMemoryServer', () => {
await mongoServer.stop({ doCleanup: false });
await mongoServer.cleanup({ doCleanup: true, force: true });
expect(utils.statPath).toHaveBeenCalledTimes(1);
expect(semver.lt).toHaveBeenCalled(); // not testing on how many, because it would change with nodejs version
expect(utils.removeDir).toHaveBeenCalled();
expect(await utils.statPath(dbPath)).toBeUndefined();
expect(mongoServer.state).toEqual(MongoMemoryServerStates.new);
expect(mongoServer.instanceInfo).toBeUndefined();
Expand Down

0 comments on commit 2f1f2e8

Please sign in to comment.