Skip to content

Commit

Permalink
Fix flaky tests (#1486)
Browse files Browse the repository at this point in the history
* Fix flaky test case

* Attempt to fix another flaky test

* lint-fix

* fix
  • Loading branch information
cspotcode committed Oct 6, 2021
1 parent 86a27be commit 8a9ae84
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/test/repl/repl-environment.spec.ts
Expand Up @@ -48,9 +48,11 @@ test.suite(
{
evalCodeBefore,
stdinCode,
waitFor,
}: {
evalCodeBefore: string | null;
stdinCode: string;
waitFor?: () => boolean;
},
assertions: (stdout: string) => Promise<void> | void
) => async (t) => {
Expand All @@ -75,7 +77,16 @@ test.suite(
replService.start();
stdin.write(stdinCode);
stdin.end();
await promisify(setTimeout)(1e3);
let done = false;
await Promise.race([
promisify(setTimeout)(20e3),
(async () => {
while (!done && !waitFor?.()) {
await promisify(setTimeout)(1e3);
}
})(),
]);
done = true;
stdout.end();
stderr.end();
expect(await getStream(stderr)).toBe('');
Expand Down Expand Up @@ -391,6 +402,7 @@ test.suite(
{
evalCodeBefore: `${setReportGlobal('repl')};${saveReportsAsGlobal}`,
stdinCode: '',
waitFor: () => !!globalInRepl.testReport,
},
(stdout) => {
expect(globalInRepl.testReport).toMatchObject({
Expand Down Expand Up @@ -434,6 +446,7 @@ test.suite(
{
evalCodeBefore: null,
stdinCode: `${setReportGlobal('repl')};${saveReportsAsGlobal}`,
waitFor: () => !!globalInRepl.testReport,
},
(stdout) => {
expect(globalInRepl.testReport).toMatchObject({
Expand Down
5 changes: 3 additions & 2 deletions src/test/repl/repl.spec.ts
Expand Up @@ -181,7 +181,7 @@ test.suite('top level await', (_test) => {
async (t) => {
const script = `
const startTime = new Date().getTime();
(async () => await new Promise((r) => setTimeout(() => r(1), ${1000})))();
(async () => await new Promise((r) => setTimeout(() => r(1), ${5000})))();
const endTime = new Date().getTime();
endTime - startTime;
`;
Expand All @@ -196,7 +196,8 @@ test.suite('top level await', (_test) => {
stdout.split('\n')[0].replace('> ', '').trim()
);
expect(ellapsedTime).toBeGreaterThanOrEqual(0);
expect(ellapsedTime).toBeLessThanOrEqual(10);
// Should ideally be instantaneous; leave wiggle-room for slow CI
expect(ellapsedTime).toBeLessThanOrEqual(100);
}
);

Expand Down

0 comments on commit 8a9ae84

Please sign in to comment.