forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunstable_wait_for_test.ts
27 lines (24 loc) · 969 Bytes
/
unstable_wait_for_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertAlmostEquals, assertEquals, assertRejects } from "@std/assert";
import { waitFor } from "./unstable_wait_for.ts";
// NOT detecting leaks means that the internal interval was correctly cleared
Deno.test("waitFor() returns fulfilled promise", async () => {
let flag = false;
setTimeout(() => flag = true, 100);
const start = Date.now();
await waitFor(() => flag === true, 1000);
// Expects the promise to be resolved after 100ms
assertAlmostEquals(Date.now() - start, 100, 10);
});
Deno.test("waitFor() throws DOMException on timeout", async () => {
let flag = false;
setTimeout(() => flag = true, 1000);
const start = Date.now();
const error = await assertRejects(
() => waitFor(() => flag === true, 100),
DOMException,
"Signal timed out.",
);
assertAlmostEquals(Date.now() - start, 100, 10);
assertEquals(error.name, "TimeoutError");
});