diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index b876a873ad..10b7256f5e 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -130,9 +130,17 @@ describe('CliRepl', () => { context('with an actual server', () => { const testServer = startTestServer('shared'); + let cliRepl: CliRepl; - it('connects to a server and interacts with it', async() => { + beforeEach(() => { cliRepl = new CliRepl(cliReplOptions); + }); + + afterEach(async() => { + await cliRepl.mongoshRepl.close(); + }); + + it('connects to a server and interacts with it', async() => { await cliRepl.start(await testServer.connectionString(), {}); output = ''; @@ -154,7 +162,6 @@ describe('CliRepl', () => { }); it('asks for a password if one is required', async() => { - cliRepl = new CliRepl(cliReplOptions); outputStream.on('data', (chunk) => { if (chunk.includes('Enter password')) { setImmediate(() => input.write('i want food\n')); @@ -174,7 +181,6 @@ describe('CliRepl', () => { }); it('respects a canceled password input', async() => { - cliRepl = new CliRepl(cliReplOptions); outputStream.on('data', (chunk) => { if (chunk.includes('Enter password')) { setImmediate(() => input.write('\u0003')); // Ctrl+C diff --git a/packages/cli-repl/src/mongosh-repl.ts b/packages/cli-repl/src/mongosh-repl.ts index a1ac11622c..71efe7aac9 100644 --- a/packages/cli-repl/src/mongosh-repl.ts +++ b/packages/cli-repl/src/mongosh-repl.ts @@ -138,7 +138,7 @@ class MongoshNodeRepl { } repl.on('exit', async() => { - await internalState.close(true); + await this.close(); this.bus.emit('mongosh:exit', 0); }); @@ -251,6 +251,15 @@ class MongoshNodeRepl { } return this._runtimeState; } + + async close(): Promise { + const rs = this._runtimeState; + if (rs) { + this._runtimeState = null; + rs.repl.close(); + await rs.internalState.close(true); + } + } } export default MongoshNodeRepl;