Skip to content

Commit f3d8a19

Browse files
committed
feat: remove all "beforeExit" listeners
fixes #563 BREAKING CHANGE: There is now no more "beforeExit" listener added
1 parent 1e1a8c8 commit f3d8a19

File tree

5 files changed

+3
-45
lines changed

5 files changed

+3
-45
lines changed

docs/api/classes/mongo-memory-replset.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ Typings: `async cleanup(force: boolean = false): Promise<void>`
8989

9090
Cleanup all files used by this ReplSet & instances
9191

92-
:::tip
93-
Runs automatically on `process.on('beforeExit')`
94-
:::
95-
9692
### waitUntilRunning
9793

9894
Typings: `async waitUntilRunning(): Promise<void>`

docs/api/classes/mongo-memory-server.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ Typings: `async cleanup(force: boolean): Promise<void>`
8181

8282
Cleanup all files used by this instance
8383

84-
:::tip
85-
Runs automatically on `process.on('beforeExit')` [This can add many listeners if many instances are used without an replset]
86-
:::
87-
8884
### ensureInstance
8985

9086
Typings: `async ensureInstance(): Promise<MongoInstanceData>`

docs/guides/faq.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ It is currently recommended to directly look at the TSDoc for these properties t
1313

1414
### Do testing database paths get cleaned up?
1515

16-
If the Database path is generated by this package, then it will automatically get cleaned up (ensured to run by process event `beforeExit`).
17-
If the Database path is provided, then it will not be automatically cleaned up, cleanup needs to be called explicitly like `.cleanup(true)` (parameter is to force-delete the directory).
16+
If the Database if a temporary directory (generated with `tmp`), then it will automatically get cleaned-up when calling `.stop()`, this can be disabled with `.stop(false)`.
17+
If the Database if manually set (set with `dbPath`), then it needs to be manually cleaned-up with `.cleanup(true)`.

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,6 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
348348
}
349349
this.stateChange(MongoMemoryReplSetStates.init); // this needs to be executed before "setImmediate"
350350

351-
// check if an "beforeExit" listener for "this.cleanup" is already defined for this class, if not add one
352-
if (
353-
process
354-
.listeners('beforeExit')
355-
.findIndex((f: (...args: any[]) => any) => f === this.cleanup) <= -1
356-
) {
357-
process.on('beforeExit', this.cleanup);
358-
}
359-
360351
await ensureAsync()
361352
.then(() => this.initAllServers())
362353
.then(() => this._initReplSet())
@@ -507,7 +498,6 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
507498

508499
/**
509500
* Remove the defined dbPath's
510-
* This function gets automatically called on process event "beforeExit" (with force being "false")
511501
* @param force Remove the dbPath even if it is no "tmpDir" (and re-check if tmpDir actually removed it)
512502
* @throws If "state" is not "stopped"
513503
* @throws If "instanceInfo" is not defined
@@ -516,7 +506,6 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
516506
async cleanup(force: boolean = false): Promise<void> {
517507
assertionIsMMSRSState(MongoMemoryReplSetStates.stopped, this._state);
518508
log(`cleanup for "${this.servers.length}" servers`);
519-
process.removeListener('beforeExit', this.cleanup);
520509

521510
await Promise.all(this.servers.map((s) => s.cleanup(force)));
522511

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,6 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
276276

277277
this.stateChange(MongoMemoryServerStates.starting);
278278

279-
// check if not replset (because MongoMemoryReplSet has an own beforeExit listener) and
280-
// check if an "beforeExit" listener for "this.cleanup" is already defined for this class, if not add one
281-
if (
282-
isNullOrUndefined(this.opts.instance?.replSet) &&
283-
process
284-
.listeners('beforeExit')
285-
.findIndex((f: (...args: any[]) => any) => f === this.cleanup) <= -1
286-
) {
287-
process.on('beforeExit', this.cleanup);
288-
}
289-
290279
await this._startUpInstance(forceSamePort).catch((err) => {
291280
if (!debug.enabled('MongoMS:MongoMemoryServer')) {
292281
console.warn('Starting the instance failed, enable debug for more information');
@@ -505,25 +494,13 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
505494

506495
/**
507496
* Remove the defined dbPath
508-
* This function gets automatically called on process event "beforeExit" (with force being "false")
509497
* @param force Remove the dbPath even if it is no "tmpDir" (and re-check if tmpDir actually removed it)
510498
* @throws If "state" is not "stopped"
511499
* @throws If "instanceInfo" is not defined
512500
* @throws If an fs error occured
513501
*/
514-
async cleanup(force: boolean): Promise<void>;
515-
/**
516-
* This Overload is used for the "beforeExit" listener (ignore this)
517-
* @internal
518-
*/
519-
async cleanup(code?: number): Promise<void>;
520-
async cleanup(force: boolean | number = false): Promise<void> {
521-
if (typeof force !== 'boolean') {
522-
force = false;
523-
}
524-
502+
async cleanup(force: boolean = false): Promise<void> {
525503
assertionIsMMSState(MongoMemoryServerStates.stopped, this.state);
526-
process.removeListener('beforeExit', this.cleanup);
527504

528505
if (isNullOrUndefined(this._instanceInfo)) {
529506
this.debug('cleanup: "instanceInfo" is undefined');

0 commit comments

Comments
 (0)