You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 6819008
Browse filesBrowse the repository at this point in the historyBrowse files
authored
fix(browser): advance fake timers in actions and expect.element (#11242)
Co-authored-by: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: docs/api/browser/assertions.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,7 @@ interface ExpectPollOptions {
55
55
```
56
56
57
57
::: tip
58
-
Like [`expect.poll`](/api/expect#poll), `expect.element` retries DOM assertions until they pass or the timeout is reached. When it receives a locator, Vitest resolves it with [`locator.findElement()`](/api/browser/locators#findelement) before running the DOM assertion. The `timeout` option applies to the whole retry operation. The `interval` option controls how often failed DOM assertions are retried, but locator resolution uses `findElement`'s own increasing retry intervals.
58
+
Like [`expect.poll`](/api/expect#poll), `expect.element` retries DOM assertions until they pass or the timeout is reached. When it receives a locator, Vitest resolves it with [`locator.findElement()`](/api/browser/locators#findelement) before running the DOM assertion. The `timeout` option applies to the whole retry operation. The `interval` option controls how often failed DOM assertions are retried, but locator resolution uses `findElement`'s own increasing retry intervals. If [fake timers](/api/vi#vi-usefaketimers) are enabled, `expect.element` advances them while it retries. See [Automatic Advancing](/guide/mocking/timers#automatic-advancing) for details.
59
59
60
60
`toMatchTextContent` and all other assertions are still available on a regular `expect` without a built-in retry-ability mechanism:
If [faketimers](/api/vi#vi-usefaketimers) areenabled, thesemethodsadvancethemwhiletheyrun. See [AutomaticAdvancing](/guide/mocking/timers#automatic-advancing) fordetails.
Copy file name to clipboardExpand all lines: docs/api/expect.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,6 +118,8 @@ The callback receives an `AbortSignal` that is aborted when the poll timeout is
118
118
119
119
If an error is thrown inside the `expect.poll` callback, Vitest will retry again until the timeout runs out.
120
120
121
+
If `vi.useFakeTimers` is used, `expect.poll` automatically calls `vi.advanceTimersByTime(interval)` after every failed attempt. See [Automatic Advancing](/guide/mocking/timers#automatic-advancing) for details.
If`vi.useFakeTimers`isused, `vi.waitUntil`automaticallycalls`vi.advanceTimersByTime(interval)`ineverycheckcallback. See [AutomaticAdvancing](/guide/mocking/timers#automatic-advancing) fordetails.
Some Vitest APIs wait for a condition to be met. When fake timers are enabled, these APIs advance them while they run:
53
+
54
+
-[`vi.waitFor`](/api/vi#vi-waitfor) and [`vi.waitUntil`](/api/vi#vi-waituntil) advance timers by `interval` before every check.
55
+
-[`expect.poll`](/api/expect#poll) advances timers by `interval` after every failed attempt.
56
+
- In Browser Mode, [`expect.element`](/api/browser/assertions) advances timers while it retries, and [locator](/api/browser/locators#methods) and [`userEvent`](/api/browser/interactivity) methods that take an element advance them while they run, even when the element already exists. The amount depends on how long the action takes in real time, so it can vary between runs. Methods that don't take an element, such as `userEvent.keyboard` and `userEvent.tab`, don't advance timers.
57
+
58
+
## Tick Modes {#tick-modes}
59
+
60
+
By default, fake timers only move when you advance them or when one of the APIs above runs. [`vi.setTimerTickMode`](/api/vi#vi-settimertickmode) makes them advance on their own:
61
+
62
+
-`interval` advances fake time by a fixed step on a real-time interval, so it stays roughly in step with real time. It suits code where some state only lasts for a limited time.
63
+
-`nextTimerAsync` runs the next pending timer after each macrotask, so chained timers fire almost back to back. It suits code that eventually settles, but state that only lasts for a limited time can disappear before an assertion or action sees it.
0 commit comments