Skip to content

Commit c8cee80

Browse files
authored
chore(cli-repl): fix flaky test and improve errors (#792)
The Node.js REPL produces errors like this when a module is not found: ```js Uncaught Error: Cannot find module 'a' Require stack: - <repl> [ ... ] ``` Our test helpers interpreted the `- <repl>` string as a prompt, which it is not. Fix that by requiring the prompt not to contain a matching angle bracket, and provide more precise error messages from our test helpers in general to help debug future problems.
1 parent 269c71e commit c8cee80

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

packages/cli-repl/test/helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ export async function eventually(fn: Function, options: { frequency?: number; ti
2121
await new Promise(resolve => setTimeout(resolve, options.frequency));
2222
}
2323

24+
Object.assign(err, {
25+
timedOut: true,
26+
timeout: options.timeout,
27+
message: `[Timed out ${options.timeout}ms] ${err.message}`
28+
});
2429
throw err;
2530
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { eventually } from './helpers';
88
export type TestShellStartupResult = { state: 'prompt' | 'exit'; exitCode?: number | undefined };
99
type SignalType = ChildProcess extends { kill: (signal: infer T) => any } ? T : never;
1010

11-
const PROMPT_PATTERN = /^([^>]*> ?)+$/m;
11+
// Assume that prompt strings are those that end in '> ' but do not contain
12+
// < or > (so that e.g. '- <repl>' in a stack trace is not considered a prompt).
13+
const PROMPT_PATTERN = /^([^<>]*> ?)+$/m;
1214
const ERROR_PATTERN_1 = /Thrown:\n([^>]*)/mg; // node <= 12.14
1315
const ERROR_PATTERN_2 = /Uncaught[:\n ]+([^>]*)/mg;
1416

@@ -133,7 +135,7 @@ export class TestShell {
133135
throw new assert.AssertionError({
134136
message: 'expected prompt',
135137
expected: PROMPT_PATTERN.toString(),
136-
actual: this._output
138+
actual: this._output.slice(0, start) + '[prompt search starts here]' + output
137139
});
138140
}
139141
});

0 commit comments

Comments
 (0)