Skip to content

Commit 553392d

Browse files
authored
chore(cli-repl): close MongoshNodeRepl in integration test (#454)
Make sure we clean up all resources even in the case of a test failure.
1 parent 0abc0fb commit 553392d

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,17 @@ describe('CliRepl', () => {
130130

131131
context('with an actual server', () => {
132132
const testServer = startTestServer('shared');
133+
let cliRepl: CliRepl;
133134

134-
it('connects to a server and interacts with it', async() => {
135+
beforeEach(() => {
135136
cliRepl = new CliRepl(cliReplOptions);
137+
});
138+
139+
afterEach(async() => {
140+
await cliRepl.mongoshRepl.close();
141+
});
142+
143+
it('connects to a server and interacts with it', async() => {
136144
await cliRepl.start(await testServer.connectionString(), {});
137145

138146
output = '';
@@ -154,7 +162,6 @@ describe('CliRepl', () => {
154162
});
155163

156164
it('asks for a password if one is required', async() => {
157-
cliRepl = new CliRepl(cliReplOptions);
158165
outputStream.on('data', (chunk) => {
159166
if (chunk.includes('Enter password')) {
160167
setImmediate(() => input.write('i want food\n'));
@@ -174,7 +181,6 @@ describe('CliRepl', () => {
174181
});
175182

176183
it('respects a canceled password input', async() => {
177-
cliRepl = new CliRepl(cliReplOptions);
178184
outputStream.on('data', (chunk) => {
179185
if (chunk.includes('Enter password')) {
180186
setImmediate(() => input.write('\u0003')); // Ctrl+C

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class MongoshNodeRepl {
138138
}
139139

140140
repl.on('exit', async() => {
141-
await internalState.close(true);
141+
await this.close();
142142
this.bus.emit('mongosh:exit', 0);
143143
});
144144

@@ -251,6 +251,15 @@ class MongoshNodeRepl {
251251
}
252252
return this._runtimeState;
253253
}
254+
255+
async close(): Promise<void> {
256+
const rs = this._runtimeState;
257+
if (rs) {
258+
this._runtimeState = null;
259+
rs.repl.close();
260+
await rs.internalState.close(true);
261+
}
262+
}
254263
}
255264

256265
export default MongoshNodeRepl;

0 commit comments

Comments
 (0)