The Node test runner’s mock timers are switched on per test with t.mock.timers.enable({ apis: ['setTimeout'] }) and moved forward with t.mock.timers.tick(ms). tick() is synchronous and fires only the timers that exist when it is called, so code that awaits between timers needs the event loop to turn between ticks. Unless we say otherwise, everything below was run on Node 24.12.0.
MockTimers has shipped in node:test since v20.4.0, and the docs mark it stable from v23.1.0. It does the job of jest.useFakeTimers() and vi.useFakeTimers(), which the Jest and Vitest docs both describe as built on @sinonjs/fake-timers. Node’s is a separate implementation, and a ported test meets the differences quickly: there is no asynchronous tick, the clock jumps before the callbacks run, and several clocks are not faked at all. If the suite has not moved to node:test yet, our look at the built-in test runner as a Jest replacement covers the rest of the migration.
How do you enable mock timers in the Node test runner?
Use the mock on the test context. t.mock.timers is reset when the test ends. The top-level mock.timers export is not: when we enabled it and left it, the next test in the file still had fake timers, and a second enable() threw MockTimers is already enabled!.
The apis array says what to replace. The docs list four values: setTimeout, setInterval, setImmediate and Date, with each timer bringing its clear function. The source accepts a fifth, scheduler.wait, and the 24.16.0 changelog adds AbortSignal.timeout. Call enable() with no arguments and the whole list is mocked, Date included, with the clock at zero: new Date().toISOString() returned 1970-01-01T00:00:00.000Z. Pass now to start somewhere else.
Name only the APIs the code under test calls. Here is a retry helper, retry.mjs, and a test that pins its backoff schedule to the millisecond.
import timers from 'node:timers/promises';
export async function retry(fn, delays = [100, 200, 400]) {
for (const delay of delays) {
try {
return await fn();
} catch {
await timers.setTimeout(delay);
}
}
return fn();
}
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { setImmediate as flush } from 'node:timers/promises';
import { retry } from './retry.mjs';
test('backs off 100, 200, then 400 ms', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
let calls = 0;
const flaky = async () => {
calls += 1;
if (calls < 4) throw new Error('503');
return 'ok';
};
const result = retry(flaky);
for (const [i, ms] of [100, 200, 400].entries()) {
await flush();
t.mock.timers.tick(ms - 1);
await flush();
assert.equal(calls, i + 1);
t.mock.timers.tick(1);
}
assert.equal(await result, 'ok');
assert.equal(calls, 4);
});
Why does only the first timer fire?
Replace that loop with one flush and a single tick(700), the sum of the three delays, and the test stalls. When we tried it, flaky had been called twice and the promise was still pending. It took three ticks, with a flush after each, to finish.
tick() runs the callbacks that are due and returns. The first sleep resolves, but the await after it continues in a microtask, and the second sleep is only created there, after tick() has returned. The start has the same gap. A tick(100) straight after calling retry() fired nothing, because the first sleep did not exist yet.
So the loop is flush, tick, repeat. The flush is setImmediate from node:timers/promises, which lets the pending promise callbacks run, and that is why setImmediate stays out of apis. The MockTimers docs list no asynchronous tick. Jest has jest.advanceTimersByTimeAsync() for this, and @sinonjs/fake-timers 15.5.0 finished the same backoff, written with the global setTimeout, in one await clock.tickAsync(700) when we ran it under node:test.
A missing tick does not fail the test. The awaited promise never settles, the default test timeout is Infinity, and with the last tick(1) deleted our test file sat silent until we killed it. With node --test --test-timeout=2000 the same file reported test timed out after 2000ms.
Which imports does the mock miss?
enable() swaps properties on globalThis and on the node:timers and node:timers/promises module objects. A function bound to a local name before that still points at the real timer. The docs carry a note that destructuring such as import { setTimeout } from 'node:timers' is “currently not supported by this API”, and import { setTimeout as sleep } from 'node:timers/promises' is the same shape.
import assert from 'node:assert/strict';
import { test } from 'node:test';
import timers, {
scheduler,
setImmediate as flush,
setTimeout as sleep,
} from 'node:timers/promises';
test('which sleeps does the mock reach?', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout', 'scheduler.wait'] });
const done = [];
timers.setTimeout(300).then(() => done.push('timers.setTimeout'));
scheduler.wait(300).then(() => done.push('scheduler.wait'));
sleep(300).then(() => done.push('named import'));
t.mock.timers.tick(300);
await flush();
assert.deepEqual(done, ['timers.setTimeout', 'scheduler.wait']);
});
The named import waited a real 300 ms. So did a CommonJS const { setTimeout: sleep } = require('node:timers/promises'). With retry.mjs rewritten to use the named import, the backoff test failed on 1 !== 2. Calling through the module object, as retry.mjs does, was mocked, and so was the global setTimeout.
scheduler.wait survives a named import because the mock replaces the wait property on the scheduler object. It has a catch of its own. On 24.12.0, once a file had mocked scheduler.wait and reset it (the no-argument enable() is enough), every later real call in that process threw TypeError [ERR_INVALID_THIS]: Value of "this" must be of type Scheduler. The restore step binds the original function to the MockTimers instance. The same lines are in the source of 24.23.0 and 26.14.0, the newest 24.x and 26.x releases on 4 December 2026, which we read but did not run. One process per test file contains the damage by default. Under --test-isolation=none it broke the next file as well.
What does tick() do differently from Jest’s fake timers?
It moves the clock the whole distance first, then runs everything that is due.
import assert from 'node:assert/strict';
import { test } from 'node:test';
test('tick() moves the clock, then runs what is due', (t) => {
t.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
const log = [];
setTimeout(() => {
log.push(`outer at ${Date.now()}`);
setTimeout(() => log.push(`inner at ${Date.now()}`), 100);
}, 100);
t.mock.timers.tick(200);
assert.deepEqual(log, ['outer at 200']);
t.mock.timers.tick(100);
assert.deepEqual(log, ['outer at 200', 'inner at 300']);
});
The outer callback sees Date.now() as 200, not 100, and the timer it creates is scheduled from 200, so it misses the tick that created it. A setInterval(fn, 100) under tick(300) ran three times and saw 300 each time. @sinonjs/fake-timers logged the outer callback at 100 and the inner one at 200 from a single tick(200) on the same program. An issue filed against 26.13.0 reports the same behaviour. It is closed, and tick() on main still advanced the clock first when we read it on 4 December 2026.
runAll() has the same limit. It advances to the furthest timer already queued and stops, so a timer created by a callback needed a second runAll(). setTime() moves Date and fires nothing: a timer due at 9999 had not run after setTime(12000), and ran on the next tick(0).
So a Jest test that jumps a long way through chained timers needs its ticks cut into the steps the code takes.
Which clocks stay real?
performance.now() and process.hrtime.bigint() did not move. After tick(60000) both had advanced by a few microseconds. Jest’s docs say its fake timers cover performance as well, so a test that measures a duration is another porting gap. process.nextTick and queueMicrotask were left alone too.
On 24.12.0 AbortSignal.timeout() is real. Asking for it in apis threw option AbortSignal.timeout is not supported, and a 50 ms signal was still live after tick(60000), then aborted on the real clock. On that version a signal made that way, as in our post on fetch timeouts in Node, keeps its real deadline whatever the test ticks. The changelogs put mock support in 24.16.0 and 26.1.0, neither of which we ran.
The mocked Timeout object is thinner than the real one. Its refresh() returns without rescheduling, so a 1000 ms timer refreshed at 600 still fired at 1000. A real timer given the same treatment fired at 1602 ms. On 24.12.0 the mock had no close() method either. The 24.16.0 changelog lists a change aligning the mock timeout API, and the 24.23.0 source has close().
When would we use it, and when not?
For timers your own code sets through an import the mock can reach, the built-in version is enough and saves a dependency. Enable named APIs through t.mock, leave setImmediate real, tick in the steps the code takes, and run with --test-timeout so a missed tick fails in seconds. Network calls in the same suite are a separate job, covered in our guide to mocking fetch in the Node test runner.
We would keep @sinonjs/fake-timers for a file that leans on refresh(), measures with performance.now(), or advances a long way through chained timers in one call. Version 15.5.0 rescheduled on refresh() and moved performance.now() when we tried both. And where a module binds its sleep by name at import, pass the sleep function in as a parameter and hand the test a fake one.
Whoooop builds Node.js back ends and takes over existing ones, scheduled jobs and dependency list included. Our Node.js development page sets out what that covers.