Skip to content

test(async): fix flaky waitFor test #6467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions async/unstable_wait_for_test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
// Copyright 2018-2025 the Deno authors. MIT license.
import { assertEquals, assertLess, assertRejects } from "@std/assert";
import { waitFor } from "./unstable_wait_for.ts";
import { FakeTime } from "@std/testing/time";

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

Deno.test("waitFor() returns fulfilled promise", async () => {
using time = new FakeTime();

let flag = false;
setTimeout(() => flag = true, 100);
const start = performance.now();
await waitFor(() => flag === true, 1000);
assertLess(performance.now() - start, 1000);
setTimeout(() => {
flag = true;
}, 100);
const startedAt = Date.now();
const promise = waitFor(() => flag, 1000);
time.tick(500);
await promise;
assertLess(Date.now() - startedAt, 1000);
});

Deno.test("waitFor() throws DOMException on timeout", async () => {
using time = new FakeTime();

let flag = false;
const id = setTimeout(() => flag = true, 1000);
const start = performance.now();
const error = await assertRejects(
() => waitFor(() => flag === true, 100),
const id = setTimeout(() => {
flag = true;
}, 1000);
const start = Date.now();
const assertion = assertRejects(
() => waitFor(() => flag, 100),
DOMException,
"Signal timed out.",
);
assertLess(performance.now() - start, 1000);
time.tick(500);
const error = await assertion;
assertLess(Date.now() - start, 1000);
assertEquals(error.name, "TimeoutError");
clearTimeout(id);
});
Loading