Skip to content

Commit 95a3861

Browse files
committed
updating timer docs
1 parent a43bb16 commit 95a3861

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

docs/JestObjectAPI.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,16 @@ This function is not available when using legacy fake timers implementation.
989989

990990
:::
991991

992+
### `jest.runToFrame()`
993+
994+
Advances all timers by the needed milliseconds to execute the next animation frame. This function 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.
999+
1000+
:::
1001+
9921002
### `jest.clearAllTimers()`
9931003

9941004
Removes any pending timers from the timer system.

docs/TimerMocks.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ it('calls the callback after 1 second via advanceTimersByTime', () => {
167167

168168
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()`.
169169

170+
## Advance Timers by frame
171+
172+
In applications, often you want to schedule work inside of an animation frame (via `requestAnimationFrame`). We expose a convenance method `jest.runToFrame()` to advance all timers enough to execute all actively scheduled animation frames.
173+
174+
```javascript
175+
jest.useFakeTimers();
176+
it('calls the animation frame callback after runToFrame()', () => {
177+
const callback = jest.fn();
178+
179+
requestAnimationFrame(callback);
180+
181+
// At this point in time, the callback should not have been called yet
182+
expect(callback).not.toBeCalled();
183+
184+
jest.runToFrame();
185+
186+
// Now our callback should have been called!
187+
expect(callback).toBeCalled();
188+
expect(callback).toHaveBeenCalledTimes(1);
189+
});
190+
```
191+
170192
## Selective Faking
171193

172194
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

Comments
 (0)