mirror of
https://github.com/coder/coder.git
synced 2026-09-23 14:03:57 +08:00
Radix's FocusScope defers its unmount event dispatch and focus restore with setTimeout(0). The cleanup() in the shared afterEach schedules that timer, and for the last test in a file it could still be pending when vitest tears down the jsdom environment. The callback then constructs a CustomEvent from Node's built-in constructor instead of jsdom's, and jsdom rejects the dispatch with "parameter 1 is not of type 'Event'" as an unhandled error, failing the run. Await one timer turn in afterAll, while the jsdom environment is still alive, to deterministically drain the 0ms timers scheduled by cleanup(). Timers with the same delay run in FIFO order, so this is not a race. Fixes coder/internal#1613
22 lines
699 B
TypeScript
22 lines
699 B
TypeScript
import { cleanup } from "@testing-library/react";
|
|
import { server } from "#/testHelpers/server";
|
|
|
|
// MSW server lifecycle
|
|
beforeAll(() => server.listen({ onUnhandledRequest: "warn" }));
|
|
afterEach(() => {
|
|
cleanup();
|
|
server.resetHandlers();
|
|
vi.clearAllMocks();
|
|
});
|
|
afterAll(async () => {
|
|
// A leftover fake clock would make the timer flush below hang.
|
|
if (vi.isFakeTimers()) {
|
|
vi.useRealTimers();
|
|
}
|
|
// Radix FocusScope defers its unmount dispatch with setTimeout(0). Flush
|
|
// it before vitest tears down the jsdom environment, where it would throw
|
|
// an unhandled "parameter 1 is not of type 'Event'" TypeError.
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
server.close();
|
|
});
|