Skip to content

fix(browser): advance fake timers in actions and expect.element - #11242

Merged
sheremet-va merged 14 commits into
vitest-dev:mainfrom
julienw:fix-fake-timers-for-browser-actions
Oct 19, 2026
Merged

sheremet-va merged 14 commits into
vitest-dev:mainfrom
julienw:fix-fake-timers-for-browser-actions

Conversation

@julienw

@julienw julienw commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

Updated description by @hi-ogawa

With fake timers enabled, a browser action such as await locator.click() times out when the element only appears after a timer such as setTimeout because nothing advances the fake clock while the action waits. expect.element has the same problem since Vitest 5, because it now uses locator.findElement in a retry loop that waits in real time, so expect.poll never gets to advance timers between attempts.

This PR fixes it by advancing fake timers while actions and element lookups wait, in the same spirit as expect.poll, vi.waitFor and vi.waitUntil. Locator actions work a bit differently from the others, because Vitest doesn't have access to the provider's actionability check loop. Instead, they advance fake timers by 50ms on a interval while the action runs (50ms is only heuristic value but taken from poll/waitFor/waitUntil's default interval).

The following table is an overview of how the fake timer advance is wired up for each API in the Playwright provider:

API Advances fake timers How
Locator click, dblClick, tripleClick, hover, unhover, clear, fill, selectOptions, upload, dropTo yes Action.#run wraps the command with advanceFakeTimersWhilePending, which advances by 50ms every 50ms while it runs, even if the element already exists
userEvent counterparts of the above, userEvent.dragAndDrop yes delegate to the locator methods
page.screenshot yes ScreenshotAction also goes through Action.#run
userEvent.type yes advanceFakeTimersWhilePending at its call site
locator.wheel, userEvent.wheel yes advanceFakeTimersWhilePending around the command and its Chromium requestAnimationFrame wait
userEvent.pointer yes advanceFakeTimersWhilePending at its call site, so inputs with a target advance while waiting for it
locator.findElement yes advances by its retry interval each time the element is missing
expect.element yes findElement while the element is missing, then expect.poll advances by interval after each failed assertion
userEvent.keyboard, tab, copy, cut, paste no they don't take an element and act on the current focus right away
locator.query, element, elements no synchronous lookups

This PR also updates the docs to describe fake timer auto-advance consistently throughout the relevant APIs.

TODO


Fixes #10058

This follows the same logic as for existing code, namely expect.poll(), and vi.waitFor and vi.waitUntil, or testing library user-event in the preview provider.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify

netlify Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 35d2e3d
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6ad5e89dad335933e6432c53
😎 Deploy Preview https://deploy-preview-11242--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@julienw

julienw commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Disclaimer: I used Claude with the Fable model, but I reviewed the generated code and made some back and forth before getting this result. Mostly because I need this fix, to be able to use fake timers in a project.

@julienw
julienw force-pushed the fix-fake-timers-for-browser-actions branch 2 times, most recently from 22309f2 to 00b710d Compare September 11, 2026 21:45
@julienw

julienw commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

ping @sheremet-va

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

Hello @julienw. Thank you for taking the time to contribute!

Unfortunately, the team accepts pull requests only from maintainers and approved contributors, so this pull request was closed automatically. We are sorry about that, it is not a judgement of your work. The number of pull requests grew beyond what the team can review, and this policy gives maintainers the space to triage and prioritize issues at their own pace.

Please keep the discussion in #10058. Your changes are not lost: a maintainer can reopen this pull request if the team decides to go forward with it. See our pull request policy for more context.

@github-actions github-actions Bot closed this Oct 5, 2026
@sheremet-va sheremet-va reopened this Oct 5, 2026
@sheremet-va

Copy link
Copy Markdown
Member

/approve-user

vitest-trust-bot Bot added a commit that referenced this pull request Oct 5, 2026
@julienw
julienw force-pushed the fix-fake-timers-for-browser-actions branch from 00b710d to 8c49ca3 Compare October 5, 2026 15:57
@julienw
julienw force-pushed the fix-fake-timers-for-browser-actions branch from 8c49ca3 to 795aad0 Compare October 5, 2026 16:07
@julienw

julienw commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Sorry I was a bit busy last week.

I rebased, removed the formatting noise, and also added a second commit to cover userEvent.wheel and userEvent.type.

hi-ogawa and others added 5 commits October 19, 2026 12:24
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hi-ogawa hi-ogawa changed the title fix(browser): advance fake timers during locator actions of browser mode fix(browser): advance fake timers in actions and expect.element Oct 19, 2026
hi-ogawa and others added 5 commits October 19, 2026 12:52
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hi-ogawa
hi-ogawa previously approved these changes Oct 19, 2026

@hi-ogawa hi-ogawa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I've expanded on test and docs.

@hi-ogawa
hi-ogawa requested a review from sheremet-va October 19, 2026 04:34
vi.advanceTimersByTime(FAKE_TIMERS_TICK)
}
}, FAKE_TIMERS_TICK)
return promise.finally(() => clearInterval(interval))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be incorrect if promise is resolved between the ticks? If it takes 120ms, fake timers will only advance 100ms

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be incorrect if promise is resolved between the ticks? If it takes 120ms, fake timers will only advance 100ms

The situation is same as any other poll/wait family. For example, poll can also end up with real 120ms and fake 100ms:

fail -> 50ms advance (both real and fake) ->
fail -> 50ms advance (both real and fake) ->
resolve after 20ms (real only)

I'm not sure if something more rigorous approach is possible. I expect advanceFakeTimersWhilePending is just a reasonable best effort behavior.

…-browser-actions

# Conflicts:
#	packages/browser/src/client/tester/context.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sheremet-va
sheremet-va merged commit 6819008 into vitest-dev:main Oct 19, 2026
27 of 29 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 3, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fake timer doesn't auto advance for browser mode actions

3 participants