test: tighten wait forever interval assertion

This commit is contained in:
Peter Steinberger
2026-05-09 05:30:01 +01:00
parent e771e251eb
commit 2fd29af86c

View File

@@ -11,11 +11,20 @@ import { waitForever } from "./wait.js";
describe("waitForever", () => {
it("creates an unref'ed interval and returns a pending promise", () => {
const setIntervalSpy = vi.spyOn(global, "setInterval");
const promise = waitForever();
expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), 1_000_000);
expect(promise).toBeInstanceOf(Promise);
setIntervalSpy.mockRestore();
const unref = vi.fn();
const interval = { unref } as unknown as ReturnType<typeof setInterval>;
const setIntervalSpy = vi.spyOn(global, "setInterval").mockReturnValue(interval);
try {
const promise = waitForever();
expect(setIntervalSpy).toHaveBeenCalledTimes(1);
const [callback, delay] = setIntervalSpy.mock.calls[0] ?? [];
expect(typeof callback).toBe("function");
expect(delay).toBe(1_000_000);
expect(unref).toHaveBeenCalledTimes(1);
expect(promise).toBeInstanceOf(Promise);
} finally {
setIntervalSpy.mockRestore();
}
});
});