Skip to content

Commit 8a9ae84

Browse files
authored
Fix flaky tests (#1486)
* Fix flaky test case * Attempt to fix another flaky test * lint-fix * fix
1 parent 86a27be commit 8a9ae84

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/test/repl/repl-environment.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ test.suite(
4848
{
4949
evalCodeBefore,
5050
stdinCode,
51+
waitFor,
5152
}: {
5253
evalCodeBefore: string | null;
5354
stdinCode: string;
55+
waitFor?: () => boolean;
5456
},
5557
assertions: (stdout: string) => Promise<void> | void
5658
) => async (t) => {
@@ -75,7 +77,16 @@ test.suite(
7577
replService.start();
7678
stdin.write(stdinCode);
7779
stdin.end();
78-
await promisify(setTimeout)(1e3);
80+
let done = false;
81+
await Promise.race([
82+
promisify(setTimeout)(20e3),
83+
(async () => {
84+
while (!done && !waitFor?.()) {
85+
await promisify(setTimeout)(1e3);
86+
}
87+
})(),
88+
]);
89+
done = true;
7990
stdout.end();
8091
stderr.end();
8192
expect(await getStream(stderr)).toBe('');
@@ -391,6 +402,7 @@ test.suite(
391402
{
392403
evalCodeBefore: `${setReportGlobal('repl')};${saveReportsAsGlobal}`,
393404
stdinCode: '',
405+
waitFor: () => !!globalInRepl.testReport,
394406
},
395407
(stdout) => {
396408
expect(globalInRepl.testReport).toMatchObject({
@@ -434,6 +446,7 @@ test.suite(
434446
{
435447
evalCodeBefore: null,
436448
stdinCode: `${setReportGlobal('repl')};${saveReportsAsGlobal}`,
449+
waitFor: () => !!globalInRepl.testReport,
437450
},
438451
(stdout) => {
439452
expect(globalInRepl.testReport).toMatchObject({

src/test/repl/repl.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ test.suite('top level await', (_test) => {
181181
async (t) => {
182182
const script = `
183183
const startTime = new Date().getTime();
184-
(async () => await new Promise((r) => setTimeout(() => r(1), ${1000})))();
184+
(async () => await new Promise((r) => setTimeout(() => r(1), ${5000})))();
185185
const endTime = new Date().getTime();
186186
endTime - startTime;
187187
`;
@@ -196,7 +196,8 @@ test.suite('top level await', (_test) => {
196196
stdout.split('\n')[0].replace('> ', '').trim()
197197
);
198198
expect(ellapsedTime).toBeGreaterThanOrEqual(0);
199-
expect(ellapsedTime).toBeLessThanOrEqual(10);
199+
// Should ideally be instantaneous; leave wiggle-room for slow CI
200+
expect(ellapsedTime).toBeLessThanOrEqual(100);
200201
}
201202
);
202203

0 commit comments

Comments
 (0)