Repository navigation
fix(browser): advance fake timers in actions and expect.element - #11242
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
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. |
22309f2 to
00b710d
Compare
|
ping @sheremet-va |
|
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. |
|
/approve-user |
Requested by @sheremet-va in #11242 (comment)
00b710d to
8c49ca3
Compare
8c49ca3 to
795aad0
Compare
|
Sorry I was a bit busy last week. I rebased, removed the formatting noise, and also added a second commit to cover |
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>
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
left a comment
There was a problem hiding this comment.
Thank you! I've expanded on test and docs.
| vi.advanceTimersByTime(FAKE_TIMERS_TICK) | ||
| } | ||
| }, FAKE_TIMERS_TICK) | ||
| return promise.finally(() => clearInterval(interval)) |
There was a problem hiding this comment.
Wouldn't this be incorrect if promise is resolved between the ticks? If it takes 120ms, fake timers will only advance 100ms
There was a problem hiding this comment.
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>
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 assetTimeoutbecause nothing advances the fake clock while the action waits.expect.elementhas the same problem since Vitest 5, because it now useslocator.findElementin a retry loop that waits in real time, soexpect.pollnever 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.waitForandvi.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 frompoll/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:
click,dblClick,tripleClick,hover,unhover,clear,fill,selectOptions,upload,dropToAction.#runwraps the command withadvanceFakeTimersWhilePending, which advances by 50ms every 50ms while it runs, even if the element already existsuserEventcounterparts of the above,userEvent.dragAndDroppage.screenshotScreenshotActionalso goes throughAction.#runuserEvent.typeadvanceFakeTimersWhilePendingat its call sitelocator.wheel,userEvent.wheeladvanceFakeTimersWhilePendingaround the command and its ChromiumrequestAnimationFramewaituserEvent.pointeradvanceFakeTimersWhilePendingat its call site, so inputs with atargetadvance while waiting for itlocator.findElementexpect.elementfindElementwhile the element is missing, thenexpect.polladvances byintervalafter each failed assertionuserEvent.keyboard,tab,copy,cut,pastelocator.query,element,elementsThis PR also updates the docs to describe fake timer auto-advance consistently throughout the relevant APIs.
TODO
pointerAPI #10780. likely just wrap__vitest_pointercallsite withadvanceFakeTimersWhilePending.Fixes #10058
This follows the same logic as for existing code, namely
expect.poll(), andvi.waitForandvi.waitUntil, or testing libraryuser-eventin the preview provider.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.