A test ported from Jest 30 to Vitest 5 defers a callback with process.nextTick, calls vi.runAllTicks() under fake timers, and now fails with zero calls. Why, and how do you fix it?
answer
- Jest fakes nextTick and queueMicrotask; Vitest does not
- vi.runAllTicks drains only a faked tick queue
- await a real tick: the queue is first in, first out
- toFake replaces the default list
- nextTick fakes need --pool=threads
basics
~20 sjest.useFakeTimers() fakes process.nextTick, so jest.runAllTicks() drained it. vi.useFakeTimers() leaves nextTick real, so vi.runAllTicks() finds nothing and the synchronous expect runs first. Await a real tick before asserting, or add 'nextTick' to toFake under the threads pool.
solid answer
~50 sBoth runners use `@sinonjs/fake-timers` but install different defaults. Jest 30 fakes every API it knows, `process.nextTick` and `queueMicrotask` included, unless you list it in `doNotFake`, and `jest.runAllTicks()` drains that fake queue. Vitest 5 fakes everything available globally **except** those two, because Node.js uses them internally. So the callback lands on the real tick queue, `vi.runAllTicks()` finds the fake queue empty, and the synchronous assertion runs first. The simplest fix is an async test that awaits `new Promise(resolve => process.nextTick(resolve))`: the tick queue is first in, first out, so the helper's callback runs before yours. The other fix is opting in with `toFake`, which **replaces** the default list, so name the timers you still need beside `'nextTick'`. Run that test with `--pool=threads`, because under the default `forks` pool Vitest throws (`is not supported in node:child_process`). Jest's `legacyFakeTimers` has no Vitest counterpart.
code
typescript · 20 lines// notify.ts
export function notifyLater(onReady: (status: string) => void): void {
process.nextTick(onReady, 'ready')
}
// notify.test.ts, renamed from the Jest version
import { afterEach, expect, test, vi } from 'vitest'
import { notifyLater } from './notify'
afterEach(() => {
vi.useRealTimers()
})
test('reports ready', () => {
vi.useFakeTimers()
const onReady = vi.fn()
notifyLater(onReady)
vi.runAllTicks() // nothing to drain: nextTick is still real
expect(onReady).toHaveBeenCalledWith('ready') // fails: 0 calls
})go deeper
Know that Vitest 5's fake timers leave process.nextTick and queueMicrotask real by default, unlike Jest 30's.
Trace why a ported runAllTicks assertion sees zero calls, and fix it with an awaited real tick or an explicit toFake list.
Port a suite's fake-timer config: rename doNotFake, timerLimit and advanceTimers, and weigh moving nextTick-faking tests to the threads pool against what threads cannot run.
Set the migration rule for tick-dependent tests: await real ticks rather than fake Node internals, and let the suite, not one fake, choose the pool.
## Same engine, different defaults Jest 30 and Vitest 5 both install fake timers from `@sinonjs/fake-timers`, and their advance and run methods behave alike. What differs is **which globals get replaced** when you call the install method with no options, and that difference is what breaks tests copied across. | Setting | Jest 30 | Vitest 5 | |---|---|---| | `process.nextTick`, `queueMicrotask` | faked | **real** unless listed in `toFake` | | Exclude some APIs | `doNotFake: [...]` | `toNotFake: [...]`, which always keeps the two above real | | Fake only a chosen set | not offered: everything is faked | `toFake: [...]`, which **replaces** the default list | | Run-all loop guard | `timerLimit`, default 100 000 | `loopLimit`, default 10 000 | | Fake clock follows real time | `advanceTimers: true` or a step in ms | `shouldAdvanceTime: true` with `advanceTimeDelta` | | Legacy implementation | `legacyFakeTimers: true` | not supported | Vitest keeps `nextTick` and `queueMicrotask` real because Node.js uses them internally. Its timer source says so in a comment: "Do not mock timers internally used by node by default." ## Why the ported test sees zero calls Take a helper that defers a callback with `process.nextTick(onReady, 'ready')`. The Jest test installed fake timers, called the helper, ran `jest.runAllTicks()` and asserted synchronously. Under Jest that passes: the tick went onto the fake queue, and `runAllTicks` drained it before the assertion. After the rename to `vi.useFakeTimers()` and `vi.runAllTicks()`, the order is: 1. The helper queues `onReady` on Node's **real** tick queue, because `nextTick` was not faked. 2. `vi.runAllTicks()` drains the fake tick queue. It is empty. 3. The synchronous `expect` runs. `onReady` has not been called, so the mock reports **0 calls**. 4. Only after the test function returns does Node run the real tick, too late for the assertion. A callback deferred with `queueMicrotask` fails the same way: Jest fakes it and `jest.runAllTicks()` flushes it, while Vitest leaves it real. ## Fix 1: wait for the real tick Make the test `async` and, before asserting, `await new Promise(resolve => process.nextTick(resolve))`. The tick queue runs in order, first in, first out, so the helper's callback runs before the one that resolves your promise. This keeps Node's internals real, works in every pool, and leaves the timer fakes as they were. It is the fix to reach for first. ## Fix 2: fake nextTick on purpose If the test must control ticks, list `'nextTick'` in `toFake`. Two traps come with it: - **`toFake` is the whole list.** `vi.useFakeTimers({ toFake: ['nextTick'] })` fakes the tick queue and nothing else, so `setTimeout` goes back to real time and every `advanceTimersByTime` call stops firing anything. Name the timers you still need, for example `['setTimeout', 'clearTimeout', 'nextTick']`. - **The default pool refuses it.** Vitest 5 runs tests in the `forks` pool, a child process whose IPC depends on `nextTick`. There the call throws an error that ends "is not supported in node:child_process. Use --pool=threads if mocking nextTick is required." Run those tests with `--pool=threads`. The pool docs warn that some native libraries can segfault in threads, and that `process.chdir()` is unavailable there, so check the suite first. ## Moving the config, not just the calls Jest reads fake-timer defaults from the `fakeTimers` key of its config, and `fakeTimers: { enableGlobally: true }` installs the fakes in every test file without a call. Vitest's `test.fakeTimers` block only holds the options that `vi.useFakeTimers()` will use; it has no `enableGlobally`, so a suite that relied on it must call `vi.useFakeTimers()` itself, in each file or in a setup file. A Jest config that kept ticks real with `doNotFake: ['nextTick']` needs no Vitest equivalent, because that is already Vitest's default. ## What carries over unchanged - `advanceTimersByTime`, `runAllTimers`, `runOnlyPendingTimers` and their `Async` twins follow the same rules as Jest's, including the need for the awaited `Async` form when the code awaits between timers. - Install the fakes before the code schedules anything, and restore real timers in `afterEach`. `vi.useRealTimers()` discards timers still queued on the fake clock; they never fire. - `now` keeps its name in both runners. New in Vitest 5, fake timers and `vi.setSystemTime` also mock `Temporal` when it exists on the global object.
- Why does Vitest leave process.nextTick real by default when Jest fakes it?Node.js uses `process.nextTick` internally, and Vitest's default `forks` pool runs tests in a child process whose communication with the main process depends on it; the docs say a mocked `nextTick` hangs there. Vitest therefore excludes `nextTick` and `queueMicrotask` from the default list, and in a child process it throws on `toFake: ['nextTick']` instead of letting the worker hang. In the `threads` pool the opt-in works.
- Which Jest fake-timer options need renaming when the config moves to Vitest?`doNotFake` becomes `toNotFake`, `timerLimit` becomes `loopLimit` (its default drops from 100 000 to 10 000), and `advanceTimers` becomes `shouldAdvanceTime` with `advanceTimeDelta` for the step. `now` keeps its name. `legacyFakeTimers` has no counterpart, because Vitest supports only the `@sinonjs/fake-timers` implementation.
- vi.runAllTicks() also flushes callbacks queued on the real process.nextTick.
- vi.useFakeTimers fakes process.nextTick and queueMicrotask by default, as Jest does.
- toFake adds APIs to the default faked set.
- Faking nextTick works the same under the forks and threads pools.
- Jest's legacyFakeTimers option carries over to Vitest.
Assumes Vitest 5 with its default forks pool, compared with Jest 30's modern fake timers; since Vitest 5.0 fake timers also mock Temporal when it is available globally.