Skip to content

Commit 29dd4d0

Browse files
authored
chore(cli-repl): work around Node.js REPL bug (#800)
In addition to the fix in c8cee80, the test here is also flaky because of a Node.js REPL bug where `repl.displayPrompt()` was called twice after we reported an error, which confused the prompt detection in the shell and made the test flaky depending on whether both prompts where received in the same output data chunk or not. For now, just wait until both parts of the double prompt are present in the output before executing the next input line.
1 parent 17d4ff2 commit 29dd4d0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/cli-repl/test/e2e.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { promises as fs, createReadStream } from 'fs';
88
import { promisify } from 'util';
99
import rimraf from 'rimraf';
1010
import path from 'path';
11-
import { readReplLogfile } from './repl-helpers';
11+
import { readReplLogfile, hasNodeBug38314 } from './repl-helpers';
1212

1313
describe('e2e', function() {
1414
const testServer = startTestServer('shared');
@@ -517,6 +517,8 @@ describe('e2e', function() {
517517
let result;
518518
result = await shell.executeLine('require("a")');
519519
expect(result).to.match(/Error: Cannot find module 'a'/);
520+
// Wait for double prompt because of Node.js REPL bug
521+
if (hasNodeBug38314()) await eventually(() => shell.assertContainsOutput('> > '));
520522
result = await shell.executeLine('require("./a")');
521523
expect(result).to.match(/^A$/m);
522524
result = await shell.executeLine('require("b")');

packages/cli-repl/test/repl-helpers.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import chai, { expect } from 'chai';
77
import sinon from 'ts-sinon';
88
import sinonChai from 'sinon-chai';
99
import chaiAsPromised from 'chai-as-promised';
10+
import repl from 'repl';
11+
import { PassThrough } from 'stream';
1012
import type { MongoshBus, MongoshBusEventsMap } from '@mongosh/types';
1113

1214
chai.use(sinonChai);
@@ -76,6 +78,17 @@ async function readReplLogfile(logPath: string) {
7678
.map((line) => JSON.parse(line));
7779
}
7880

81+
// https://github.com/nodejs/node/pull/38314
82+
function hasNodeBug38314() {
83+
const input = new PassThrough();
84+
const output = new PassThrough();
85+
const evalFn = (code, ctx, filename, cb) => cb(new Error('err'));
86+
const prompt = 'prompt#';
87+
repl.start({ input, output, eval: evalFn, prompt });
88+
input.end('s\n');
89+
return String(output.read()).includes('prompt#prompt#');
90+
}
91+
7992
export {
8093
expect,
8194
sinon,
@@ -85,5 +98,6 @@ export {
8598
waitEval,
8699
waitCompletion,
87100
fakeTTYProps,
88-
readReplLogfile
101+
readReplLogfile,
102+
hasNodeBug38314
89103
};

0 commit comments

Comments
 (0)