|
1 | 1 | // Copyright 2018-2025 the Deno authors. MIT license.
|
2 | 2 | import { assertEquals, assertLess, assertRejects } from "@std/assert";
|
3 | 3 | import { waitFor } from "./unstable_wait_for.ts";
|
| 4 | +import { FakeTime } from "@std/testing/time"; |
4 | 5 |
|
5 | 6 | // NOT detecting leaks means that the internal interval was correctly cleared
|
6 | 7 |
|
7 | 8 | Deno.test("waitFor() returns fulfilled promise", async () => {
|
| 9 | + using time = new FakeTime(); |
| 10 | + |
8 | 11 | 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); |
13 | 20 | });
|
14 | 21 |
|
15 | 22 | Deno.test("waitFor() throws DOMException on timeout", async () => {
|
| 23 | + using time = new FakeTime(); |
| 24 | + |
16 | 25 | 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), |
21 | 32 | DOMException,
|
22 | 33 | "Signal timed out.",
|
23 | 34 | );
|
24 |
| - assertLess(performance.now() - start, 1000); |
| 35 | + time.tick(500); |
| 36 | + const error = await assertion; |
| 37 | + assertLess(Date.now() - start, 1000); |
25 | 38 | assertEquals(error.name, "TimeoutError");
|
26 | 39 | clearTimeout(id);
|
27 | 40 | });
|
0 commit comments