Exposing jest.runToFrame() from sinon/fake_timers - #14598
Conversation
â Deploy Preview for jestjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
|
|
||
| ::: | ||
|
|
||
| ### `jest.runToFrame()` |
There was a problem hiding this comment.
Should I add these changes to 29.7 docs or just the base docs like I have now?
|
Update: never mind, I found where this check should happen (jest runtime). I have updated the PR @SimenB you mentioned that
I can add that, but I did not see any other cases of that so I have not for now. I can add that if you still want it |
|
It looks like the build failures are caused by unrelated issues |
SimenB
left a comment
There was a problem hiding this comment.
Perfect, thanks!
In addition to my API question, could you add a changelog entry? ð
|
|
||
| ::: | ||
|
|
||
| ### `jest.runToFrame()` |
There was a problem hiding this comment.
Should we call it runToNextFrame? runToFrame sounds to me like it'd take which frame (or a number of frames) as an argument
There was a problem hiding this comment.
I just mirrored the sinon api without too much thought ð
runToNextFrame() feels nice and fits with runOnlyPendingTimers().
Another idea: advanceTimersToNextFrame() - fits in with `advanceTimersToNextTimer()
Which do you lean towards? (Or something else?)
There was a problem hiding this comment.
yeah, advanceTimersToNextFrame sounds perfect to align with existing functions ð
There was a problem hiding this comment.
hmm? I believe I answered the question? ð
There was a problem hiding this comment.
Sorry, for some reason I didn't see your reply
Yeah, the windows tests has a bit of flake, unfortunately |
| * It is recommended to use `jest.mock()` instead. The `jest.mock()` API's second | ||
| * argument is a module factory instead of the expected exported module object. | ||
| */ | ||
| /** |
There was a problem hiding this comment.
The above TSDoc belongs to 'setMock'. Would be good to move it down (;
|
Hi @SimenB, I think I have addressed all the open feedback. Please take a look and let me know what you think |
|
|
||
| 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. | ||
|
|
||
| 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`. |
There was a problem hiding this comment.
A added some more detail here to hopefully make things clearer for consumers
|
Here is a workaround for anybody who needs it before // ensure you are using "modern" fake timers
// 1. before doing anything, grab the start time `setStartSystemTime()`
// 2. step through frames by using `advanceTimersToNextFrame()`
let startTime: number | null = null;
/** Record the initial (mocked) system start time
*
* This is no longer needed once `jest.advanceTimersToNextFrame()` is available
* https://github.com/jestjs/jest/pull/14598
*/
export function setStartSystemTime() {
startTime = Date.now();
}
/** Step forward a single animation frame
*
* This is no longer needed once `jest.advanceTimersToNextFrame()` is available
* https://github.com/jestjs/jest/pull/14598
*/
export function advanceTimersToNextFrame() {
if(startTime == null) {
throw new Error('Must call `setStartSystemTime` before using `advanceTimersToNextFrame()`');
}
// Stealing logic from sinon fake timers
// https://github.com/sinonjs/fake-timers/blob/fc312b9ce96a4ea2c7e60bb0cccd2c604b75cdbd/src/fake-timers-src.js#L1102-L1105
const timePassedInFrame = (Date.now() - startTime) % 16;
const timeToNextFrame = 16 - timePassedInFrame;
jest.advanceTimersByTime(timeToNextFrame);
} |
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Closes #14593