Skip to content

deferred-assertion ​

An assertion in a callback that runs after the test has ended, or only if something happens: a timer (setTimeout, setInterval, setImmediate, queueMicrotask, requestAnimationFrame, process.nextTick), an event listener (addEventListener, .on, .once, ...), or a promise's .then / .catch / .finally that the test neither awaits nor returns. Reported in test files. On by default, as an error.

Why: the runner ends the test when its function returns (or its promise settles). An assertion that runs later fails outside every test: depending on the runner it is reported as an error of the whole run against no test, or lost when the process exits first, and a listener that never fires runs no assertion at all. Guide: Testing.

❌ Rejected: the test has passed before the promise settles

✕ Rejecteddeferred-assertion
ts
import assert from "node:assert/strict";
import { test } from "node:test";

async function load(id: string): Promise<{ readonly id: string; readonly size: number }> {
  await Promise.resolve();
  return { id, size: id.length };
}

test("load reads the size", () => {
  void load("abc").then((item) => {
    assert.equal(item.size, 3);
  });
});

✅ Accepted: the test awaits the value, then asserts

✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";

async function load(id: string): Promise<{ readonly id: string; readonly size: number }> {
  await Promise.resolve();
  return { id, size: id.length };
}

test("load reads the size", async () => {
  const item = await load("abc");
  assert.equal(item.size, 3);
});

What is waited for ​

  • An awaited or returned expression, with every callback in it: await p.then(...), return p.then(...), await Promise.all(list.map(async (x) => ...)), an arrow's expression body.
  • await new Promise((resolve) => setTimeout(() => { ...; resolve(); }, ms)): the executor runs right away and the test waits for what it settles, timers in it too.
  • A promise kept in a const that the test uses again (const done = p.then(...); await done).
  • Fake timers moved in the test (vi.runAllTimers(), vi.advanceTimersByTime(ms), jest.runAllTimers(), mock.timers.tick(ms), clock.tick(ms)): the timer's callback runs inside that call.

For a listener, record what it sees and assert after the event, or wait for it: await once(emitter, "ready").

What the rule can't see ​

  • A callback stored first and handed to a timer by name (const later = () => ...; setTimeout(later)).
  • A project helper that runs its callback later: callbacks handed to the project's own functions are taken to run there.
  • An event fired synchronously by the test (el.dispatchEvent(...) right after addEventListener) is still reported: the assertion runs only if the event fires.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra