Skip to content
Sarj Code Standards

no-unsignaled-poll

Await the event a test depends on instead of polling spy or mock state until a deadline.

Why

Polling a spy races the code under test against the poll deadline: under CI load the call lands after the deadline and the test fails, and a negated call assertion passes before the call can happen.

Fix

Await the operation that makes the call, or have the test double resolve a promise when it is called and await that promise before asserting on the spy.

Examples

Before — flagged Do not poll a spy until a deadline
src/save.test.ts
import { expect, it, vi } from "vitest";
it("saves", async () => {
const save = vi.fn();
submit(save);
await vi.waitFor(() => expect(save).toHaveBeenCalledOnce());
});
After — preferred Await the promise the test double resolves
src/save.test.ts
import { expect, it, vi } from "vitest";
it("saves", async () => {
const called = Promise.withResolvers<void>();
const save = vi.fn(() => {
called.resolve();
});
submit(save);
await called.promise;
expect(save).toHaveBeenCalledOnce();
});
Before — flagged Do not poll recorded mock calls
src/save.test.ts
import { expect, it, vi } from "vitest";
it("saves", async () => {
const save = vi.fn();
submit(save);
await expect.poll(() => save.mock.calls.length).toBe(1);
});
After — preferred Polling observable application state is outside this rule
src/save.test.ts
import { expect, it, vi } from "vitest";
it("saves", async () => {
submit();
await vi.waitFor(() => expect(status()).toBe("saved"));
});

References