Skip to content

Commit

Permalink
feat: remove support for ".cleanup(boolean)"
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
".cleanup(boolean)" is now no longer supported, use object options instead
  • Loading branch information
hasezoey committed May 8, 2023
1 parent f925498 commit 9bff82e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 37 deletions.
18 changes: 3 additions & 15 deletions packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,35 +554,23 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
return true;
}

/**
* Remove the defined dbPath's
* @param force Remove the dbPath even if it is no "tmpDir" (and re-check if tmpDir actually removed it)
* @throws If "state" is not "stopped"
* @throws If "instanceInfo" is not defined
* @throws If an fs error occured
*
* @deprecated replace argument with `Cleanup` interface object
*/
async cleanup(force: boolean): Promise<void>; // TODO: for next major release (9.0), this should be removed
/**
* Remove the defined dbPath's
* @param options Set how to run a cleanup, by default `{ doCleanup: true }` is used
* @throws If "state" is not "stopped"
* @throws If "instanceInfo" is not defined
* @throws If an fs error occured
*/
async cleanup(options?: Cleanup): Promise<void>;
async cleanup(options?: boolean | Cleanup): Promise<void> {
async cleanup(options?: Cleanup): Promise<void> {
assertionIsMMSRSState(MongoMemoryReplSetStates.stopped, this._state);
log(`cleanup for "${this.servers.length}" servers`);

/** Default to doing cleanup, but not forcing it */
let cleanup: Cleanup = { doCleanup: true, force: false };

// handle the old way of setting wheter to cleanup or not
// TODO: for next major release (9.0), this should be removed
// TODO: for next major release (10.0), this should be removed
if (typeof options === 'boolean') {
cleanup.force = options;
throw new Error('Unsupported argument type: boolean');
}

// handle the new way of setting what and how to cleanup
Expand Down
18 changes: 3 additions & 15 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,34 +530,22 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
return true;
}

/**
* Remove the defined dbPath
* @param force Remove the dbPath even if it is no "tmpDir" (and re-check if tmpDir actually removed it)
* @throws If "state" is not "stopped"
* @throws If "instanceInfo" is not defined
* @throws If an fs error occured
*
* @deprecated replace argument with `Cleanup` interface object
*/
async cleanup(force: boolean): Promise<void>; // TODO: for next major release (9.0), this should be removed
/**
* Remove the defined dbPath
* @param options Set how to run a cleanup, by default `{ doCleanup: true }` is used
* @throws If "state" is not "stopped"
* @throws If "instanceInfo" is not defined
* @throws If an fs error occured
*/
async cleanup(options?: Cleanup): Promise<void>;
async cleanup(options?: boolean | Cleanup): Promise<void> {
async cleanup(options?: Cleanup): Promise<void> {
assertionIsMMSState(MongoMemoryServerStates.stopped, this.state);

/** Default to doing cleanup, but not forcing it */
let cleanup: Cleanup = { doCleanup: true, force: false };

// handle the old way of setting wheter to cleanup or not
// TODO: for next major release (9.0), this should be removed
// TODO: for next major release (10.0), this should be removed
if (typeof options === 'boolean') {
cleanup.force = options;
throw new Error('Unsupported argument type: boolean');
}

// handle the new way of setting what and how to cleanup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,9 @@ describe('MongoMemoryReplSet', () => {
.mockResolvedValue(void 0);

await replSet.stop({ doCleanup: false });
await replSet.cleanup(false);
await replSet.cleanup({ doCleanup: true, force: false });

expect(cleanupSpy).toHaveBeenCalledWith(false);
expect(cleanupSpy).toHaveBeenCalledWith({ doCleanup: true, force: false });
expect(cleanupInstance0Spy).toHaveBeenCalledWith({
doCleanup: true,
force: false,
Expand All @@ -675,13 +675,29 @@ describe('MongoMemoryReplSet', () => {
.mockResolvedValue(void 0);

await replSet.stop({ doCleanup: false });
await replSet.cleanup(true);
await replSet.cleanup({ doCleanup: true, force: true });
expect(cleanupInstance1Spy).toHaveBeenCalledWith({
doCleanup: true,
force: true,
} as utils.Cleanup);
});

it('should not support boolean arguments', async () => {
const replSet = new MongoMemoryReplSet();

try {
await replSet.cleanup(
// @ts-expect-error Testing a non-existing overload
true
);
fail('Expected to fail');
} catch (err) {
expect(err).toBeInstanceOf(Error);
assertIsError(err);
expect(err.message).toMatchSnapshot();
}
});

it('should run cleanup with cleanup options and pass-through options to lower', async () => {
const replSet = await MongoMemoryReplSet.create({ replSet: { count: 1 } });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ describe('MongoMemoryServer', () => {
tmpdir = mongoServer.instanceInfo?.tmpDir;

await mongoServer.stop({ doCleanup: false });
await mongoServer.cleanup(true);
await mongoServer.cleanup({ doCleanup: true, force: true });

expect(utils.statPath).toHaveBeenCalledTimes(1);
expect(utils.removeDir).toHaveBeenCalled();
Expand All @@ -839,7 +839,7 @@ describe('MongoMemoryServer', () => {
tmpdir = mongoServer.instanceInfo?.tmpDir;

await mongoServer.stop({ doCleanup: false });
await mongoServer.cleanup(true);
await mongoServer.cleanup({ doCleanup: true, force: true });

expect(utils.statPath).toHaveBeenCalledTimes(1);
expect(utils.removeDir).toHaveBeenCalled();
Expand All @@ -848,6 +848,22 @@ describe('MongoMemoryServer', () => {
expect(mongoServer.instanceInfo).toBeUndefined();
});

it('should not support boolean arguments', async () => {
const mongoServer = new MongoMemoryServer();

try {
await mongoServer.cleanup(
// @ts-expect-error Testing a non-existing overload
true
);
fail('Expected to fail');
} catch (err) {
expect(err).toBeInstanceOf(Error);
assertIsError(err);
expect(err.message).toMatchSnapshot();
}
});

it('should properly cleanup with tmpDir with default no force (new)', async () => {
const mongoServer = await MongoMemoryServer.create();
const dbPath = mongoServer.instanceInfo!.dbPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`MongoMemoryReplSet "_waitForPrimary" should throw an error if timeout is reached 1`] = `"{\\"timeout\\":1}"`;

exports[`MongoMemoryReplSet .cleanup() should not support boolean arguments 1`] = `"Unsupported argument type: boolean"`;

exports[`MongoMemoryReplSet .stop() should not support boolean arguments 1`] = `"Unsupported argument type: boolean"`;

exports[`MongoMemoryReplSet getters & setters setter of "replSetOpts" should throw an error if count is 1 or above 1`] = `"ReplSet Count needs to be 1 or higher! (specified count: \\"0\\")"`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

exports[`MongoMemoryServer "createAuth" should throw an error if called without "this.auth" defined 1`] = `"\\"createAuth\\" got called, but \\"this.auth\\" is undefined!"`;

exports[`MongoMemoryServer cleanup() should not support boolean arguments 1`] = `
"Incorrect State for operation: \\"new\\", allowed States: \\"[stopped]\\"
This may be because of using a v6.x way of calling functions, look at the following guide if anything applies:
https://nodkz.github.io/mongodb-memory-server/docs/guides/migration/migrate7#no-function-other-than-start-create-ensureinstance-will-be-starting-anything"
`;

exports[`MongoMemoryServer cleanup() should throw an error if state is not "stopped" 1`] = `
"Incorrect State for operation: \\"new\\", allowed States: \\"[stopped]\\"
This may be because of using a v6.x way of calling functions, look at the following guide if anything applies:
Expand Down
2 changes: 0 additions & 2 deletions packages/mongodb-memory-server-core/src/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,6 @@ export abstract class ManagerBase {
*/
export abstract class ManagerAdvanced extends ManagerBase {
abstract getUri(otherDB?: string | boolean): string;
/** @deprecated replace argument with `Cleanup` interface object */
abstract cleanup(force: boolean): Promise<void>; // TODO: for next major release (9.0), this should be removed
abstract cleanup(cleanup: Cleanup): Promise<void>;
}

Expand Down

0 comments on commit 9bff82e

Please sign in to comment.