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
-`[jest-environment-jsdom]`[**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
10
10
-`[@jest/fake-timers]`[**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544))
11
+
-`[@jest/fake-timers]` Exposing new modern timers function `advanceTimersToFrame()` which advances all timers by the needed milliseconds to execute callbacks currently scheduled with `requestAnimationFrame` ([#14598](https://github.com/jestjs/jest/pull/14598))
12
+
-`[jest-runtime]` Exposing new modern timers function `jest.advanceTimersToFrame()` from `@jest/fake-timers` ([#14598](https://github.com/jestjs/jest/pull/14598))
11
13
-`[@jest/schemas]` Upgrade `@sinclair/typebox` to v0.31 ([#14072](https://github.com/jestjs/jest/pull/14072))
12
14
-`[@jest/types]``test.each()`: Accept a readonly (`as const`) table properly ([#14565](https://github.com/jestjs/jest/pull/14565))
13
15
-`[jest-snapshot]`[**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965))
Copy file name to clipboardExpand all lines: docs/JestObjectAPI.md
+10
Original file line number
Diff line number
Diff line change
@@ -989,6 +989,16 @@ This function is not available when using legacy fake timers implementation.
989
989
990
990
:::
991
991
992
+
### `jest.advanceTimersToNextFrame()`
993
+
994
+
Advances all timers by the needed milliseconds to execute callbacks currently scheduled with `requestAnimationFrame`. `advanceTimersToNextFrame()` is a helpful way to execute code that is scheduled using `requestAnimationFrame`.
995
+
996
+
:::info
997
+
998
+
This function is not available when using legacy fake timers implementation.
Copy file name to clipboardExpand all lines: docs/TimerMocks.md
+24
Original file line number
Diff line number
Diff line change
@@ -167,6 +167,30 @@ it('calls the callback after 1 second via advanceTimersByTime', () => {
167
167
168
168
Lastly, it may occasionally be useful in some tests to be able to clear all of the pending timers. For this, we have `jest.clearAllTimers()`.
169
169
170
+
## Advance Timers to the next Frame
171
+
172
+
In applications, often you want to schedule work inside of an animation frame (with `requestAnimationFrame`). We expose a convenience method `jest.advanceTimersToNextFrame()` to advance all timers enough milliseconds to execute all actively scheduled animation frames.
173
+
174
+
For mock timing purposes, animation frames are executed every `16ms` (mapping to roughly `60` frames per second) after the clock starts. When you schedule a callback in an animation frame (with `requestAnimationFrame(callback)`), the `callback` will be called when the clock has advanced `16ms`. `jest.advanceTimersToNextFrame()` will advance the clock just enough to get to the next `16ms` increment. If the clock has already advanced `6ms` since a animation frame `callback` was scheduled, then the clock will be advanced by `10ms`.
175
+
176
+
```javascript
177
+
jest.useFakeTimers();
178
+
it('calls the animation frame callback after advanceTimersToNextFrame()', () => {
179
+
constcallback=jest.fn();
180
+
181
+
requestAnimationFrame(callback);
182
+
183
+
// At this point in time, the callback should not have been called yet
184
+
expect(callback).not.toBeCalled();
185
+
186
+
jest.advanceTimersToNextFrame();
187
+
188
+
// Now our callback should have been called!
189
+
expect(callback).toBeCalled();
190
+
expect(callback).toHaveBeenCalledTimes(1);
191
+
});
192
+
```
193
+
170
194
## Selective Faking
171
195
172
196
Sometimes your code may require to avoid overwriting the original implementation of one or another API. If that is the case, you can use `doNotFake` option. For example, here is how you could provide a custom mock function for `performance.mark()` in jsdom environment:
0 commit comments