What this thread does
Do first: create userEvent.setup({ advanceTimers: (ms) => { vi.advanceTimersByTime(ms) } }) after enabling Vitest fake timers.
Short answer
As of 2026-11-19, the release references are user-event 14.6.13 and Vitest 5.1.1. Between some subsequent inputs, code execution is delayed per setTimeout. The documented recommendation starts at v14.1: provide clock advancement rather than suppressing timeouts with delay: null.
Choose / don't choose
- Fake clock: choose for application timer behavior; advance the interaction clock and the application's timer deliberately.
- Real clock: choose when timer control is unnecessary.
- Persisting hang: investigate the runner/environment integration before increasing timeouts. Issue #1115 reports that
advanceTimersdid not work; it is not evidence of a universal fixed-in release.
Not for: network response mocking; see mock-third-party-apis-msw-nock-go-tests.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
advanceTimers | `(delay: number) => Promise<void> | void; pass a wrapper calling vi.advanceTimersByTime(ms)` | user-event 14.6.13 / Vitest 5.1.1 |
delay | Delay execution between subsequent inputs with setTimeout; null removes scheduled steps and is discouraged as a timeout workaround | user-event 14.6.13; recommendation since 14.1 | source |
vi.runOnlyPendingTimersAsync() | Asynchronously calls pending timers, including asynchronous ones | Vitest 5.1.1 current API | source |
vi.useRealTimers() | Restore only after interactions settle and pending fake timers are flushed | Vitest fake-clock teardown | source; source |
Order of operations
1. Enable fake timers, then create the user instance for the test. Keep the same instance for its interactions. 2. Await user.click() and subsequent interactions. Advance the application's debounce/countdown separately before its state assertions; the user-event callback does not mean the application timer has completed. 3. At teardown, await interactions and component cleanup that may schedule timers, flush existing timers, then restore real timers. The teardown guide says to flush pending timers before switching to real timers. 4. With asynchronous timer callbacks, use the async pending-timer API. Put restoration in a final cleanup path so a failed assertion does not leave fake timers installed.
Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|---|---|---|
It makes the test timed out | The report cannot use await user.click() | Configure advanceTimers, retain numeric delay, then inspect the minimal runner integration if it still stalls | source |
| Scheduled callbacks disappear or affect later tests (symptom) | Switching to real timers without progressing them leaves scheduled tasks unexecuted | Flush pending callbacks first, then restore real timers in teardown | source |
Missing
- The documents date the recommendation to 14.1; they do not establish the earliest implementation commit of the option.
- Issue #1115 remains a historical integration report; its community global-Jest shims are not a universal Vitest requirement.
Checked
2026-11-19; linked sources read. No application commands executed.
Started from a post by @Testing Library, packaged by the editor. Original post