Skip to content

Commit 23358a6

Browse files
authored
test(async): fix flaky waitFor test (#6467)
1 parent 32f55fd commit 23358a6

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

async/unstable_wait_for_test.ts

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
// Copyright 2018-2025 the Deno authors. MIT license.
22
import { assertEquals, assertLess, assertRejects } from "@std/assert";
33
import { waitFor } from "./unstable_wait_for.ts";
4+
import { FakeTime } from "@std/testing/time";
45

56
// NOT detecting leaks means that the internal interval was correctly cleared
67

78
Deno.test("waitFor() returns fulfilled promise", async () => {
9+
using time = new FakeTime();
10+
811
let flag = false;
9-
setTimeout(() => flag = true, 100);
10-
const start = performance.now();
11-
await waitFor(() => flag === true, 1000);
12-
assertLess(performance.now() - start, 1000);
12+
setTimeout(() => {
13+
flag = true;
14+
}, 100);
15+
const startedAt = Date.now();
16+
const promise = waitFor(() => flag, 1000);
17+
time.tick(500);
18+
await promise;
19+
assertLess(Date.now() - startedAt, 1000);
1320
});
1421

1522
Deno.test("waitFor() throws DOMException on timeout", async () => {
23+
using time = new FakeTime();
24+
1625
let flag = false;
17-
const id = setTimeout(() => flag = true, 1000);
18-
const start = performance.now();
19-
const error = await assertRejects(
20-
() => waitFor(() => flag === true, 100),
26+
const id = setTimeout(() => {
27+
flag = true;
28+
}, 1000);
29+
const start = Date.now();
30+
const assertion = assertRejects(
31+
() => waitFor(() => flag, 100),
2132
DOMException,
2233
"Signal timed out.",
2334
);
24-
assertLess(performance.now() - start, 1000);
35+
time.tick(500);
36+
const error = await assertion;
37+
assertLess(Date.now() - start, 1000);
2538
assertEquals(error.name, "TimeoutError");
2639
clearTimeout(id);
2740
});

0 commit comments

Comments
 (0)