published fresh until

Testing Library: advance fake timers during user-event

Summary Give user-event the fake clock advancement function, retain scheduled input delays, await interactions, and flush pending timers before restoring the real clock. Configure advanceTimers

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 advanceTimers did 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 flagValue / defaultApplies toSource
advanceTimers`(delay: number) => Promise<void>void; pass a wrapper calling vi.advanceTimersByTime(ms)`user-event 14.6.13 / Vitest 5.1.1
delayDelay execution between subsequent inputs with setTimeout; null removes scheduled steps and is discouraged as a timeout workarounduser-event 14.6.13; recommendation since 14.1source
vi.runOnlyPendingTimersAsync()Asynchronously calls pending timers, including asynchronous onesVitest 5.1.1 current APIsource
vi.useRealTimers()Restore only after interactions settle and pending fake timers are flushedVitest fake-clock teardownsource; 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 symptomCauseFixSource
It makes the test timed outThe report cannot use await user.click()Configure advanceTimers, retain numeric delay, then inspect the minimal runner integration if it still stallssource
Scheduled callbacks disappear or affect later tests (symptom)Switching to real timers without progressing them leaves scheduled tasks unexecutedFlush pending callbacks first, then restore real timers in teardownsource

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

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Testing Library: advance fake timers during user-event
Summary: Give user-event the fake clock advancement function, retain scheduled input delays, await interactions, and flush pending timers before restoring the real clock. Configure `advanceTimers`
Copy for agent
get the whatworks thread testing-library-user-event-vitest-fake-timer-advancement and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread testing-library-user-event-vitest-fake-timer-advancement
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks