Skip to content

Commit e7e5543

Browse files
committed
renaming to jest.advanceTimersToNextFrame
1 parent 2dd1cb3 commit e7e5543

File tree

7 files changed

+41
-39
lines changed

7 files changed

+41
-39
lines changed

docs/JestObjectAPI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ This function is not available when using legacy fake timers implementation.
989989

990990
:::
991991

992-
### `jest.runToFrame()`
992+
### `jest.advanceTimersToNextFrame()`
993993

994994
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`.
995995

docs/TimerMocks.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,23 @@ 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 to next Frame
170+
## Advance Timers to the next Frame
171171

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 milliseconds to execute all actively scheduled animation frames.
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`.
173175

174176
```javascript
175177
jest.useFakeTimers();
176-
it('calls the animation frame callback after runToFrame()', () => {
178+
it('calls the animation frame callback after advanceTimersToNextFrame()', () => {
177179
const callback = jest.fn();
178180

179181
requestAnimationFrame(callback);
180182

181183
// At this point in time, the callback should not have been called yet
182184
expect(callback).not.toBeCalled();
183185

184-
jest.runToFrame();
186+
jest.advanceTimersToNextFrame();
185187

186188
// Now our callback should have been called!
187189
expect(callback).toBeCalled();

packages/jest-environment/src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ export interface Jest {
6767
* Not available when using legacy fake timers implementation.
6868
*/
6969
advanceTimersByTimeAsync(msToRun: number): Promise<void>;
70+
/**
71+
* Advances all timers by the needed milliseconds to execute currently scheduled animation frames.
72+
* `advanceTimersToNextFrame()` helpful way to execute code that is scheduled using `requestAnimationFrame`.
73+
*
74+
* @remarks
75+
* Not available when using legacy fake timers implementation.
76+
*/
77+
advanceTimersToNextFrame(): void;
7078
/**
7179
* Advances all timers by the needed milliseconds so that only the next
7280
* timeouts/intervals will run. Optionally, you can provide steps, so it will
@@ -349,14 +357,6 @@ export interface Jest {
349357
* It is recommended to use `jest.mock()` instead. The `jest.mock()` API's second
350358
* argument is a module factory instead of the expected exported module object.
351359
*/
352-
/**
353-
* Advances all timers by the needed milliseconds to execute the next animation frame.
354-
* `runToFrame()` helpful way to execute code that is scheduled using `requestAnimationFrame`.
355-
*
356-
* @remarks
357-
* Not available when using legacy fake timers implementation.
358-
*/
359-
runToFrame(): void;
360360
setMock(moduleName: string, moduleExports: unknown): Jest;
361361
/**
362362
* Set the current system time used by fake timers. Simulates a user changing

packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ describe('FakeTimers', () => {
594594
});
595595
});
596596

597-
describe('runToFrame', () => {
597+
describe('advanceTimersToNextFrame', () => {
598598
it('runs scheduled animation frames in order', () => {
599599
const global = {
600600
Date,
@@ -616,7 +616,7 @@ describe('FakeTimers', () => {
616616
global.requestAnimationFrame(mock2);
617617
global.requestAnimationFrame(mock3);
618618

619-
timers.runToFrame();
619+
timers.advanceTimersToNextFrame();
620620

621621
expect(runOrder).toEqual(['mock1', 'mock2', 'mock3']);
622622
});
@@ -644,11 +644,11 @@ describe('FakeTimers', () => {
644644
global.requestAnimationFrame(run);
645645

646646
// only the first frame should be executed
647-
timers.runToFrame();
647+
timers.advanceTimersToNextFrame();
648648

649649
expect(runOrder).toEqual(['first-frame']);
650650

651-
timers.runToFrame();
651+
timers.advanceTimersToNextFrame();
652652

653653
expect(runOrder).toEqual(['first-frame', 'second-frame']);
654654
});
@@ -673,7 +673,7 @@ describe('FakeTimers', () => {
673673
global.cancelAnimationFrame(timerId);
674674

675675
// no frames should be executed
676-
timers.runToFrame();
676+
timers.advanceTimersToNextFrame();
677677

678678
expect(runOrder).toEqual([]);
679679
});
@@ -705,7 +705,7 @@ describe('FakeTimers', () => {
705705
expect(runOrder).toEqual([]);
706706

707707
// move timers forward to execute frame
708-
timers.runToFrame();
708+
timers.advanceTimersToNextFrame();
709709

710710
// frame has executed as time has moved forward 10ms to get to the 16ms frame time
711711
expect(runOrder).toEqual(['frame']);
@@ -733,7 +733,7 @@ describe('FakeTimers', () => {
733733
global.setTimeout(() => runOrder.push('timeout'), 10);
734734

735735
// move timers forward to execute frame
736-
timers.runToFrame();
736+
timers.advanceTimersToNextFrame();
737737

738738
expect(runOrder).toEqual(['timeout', 'frame']);
739739
});
@@ -759,7 +759,7 @@ describe('FakeTimers', () => {
759759
global.setTimeout(() => runOrder.push('timeout'), 1);
760760
});
761761

762-
timers.runToFrame();
762+
timers.advanceTimersToNextFrame();
763763

764764
// timeout not yet executed
765765
expect(runOrder).toEqual(['frame']);

packages/jest-fake-timers/src/modernFakeTimers.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ export default class FakeTimers {
110110
}
111111
}
112112

113-
runAllTicks(): void {
113+
advanceTimersToNextFrame(): void {
114114
if (this._checkFakeTimers()) {
115-
// @ts-expect-error - doesn't exist?
116-
this._clock.runMicrotasks();
115+
this._clock.runToFrame();
117116
}
118117
}
119118

120-
runToFrame(): void {
119+
runAllTicks(): void {
121120
if (this._checkFakeTimers()) {
122-
this._clock.runToFrame();
121+
// @ts-expect-error - doesn't exist?
122+
this._clock.runMicrotasks();
123123
}
124124
}
125125

packages/jest-runtime/src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,16 @@ export default class Runtime {
22892289
);
22902290
}
22912291
},
2292+
advanceTimersToNextFrame: () => {
2293+
const fakeTimers = _getFakeTimers();
2294+
2295+
if (fakeTimers === this._environment.fakeTimersModern) {
2296+
return fakeTimers.advanceTimersToNextFrame();
2297+
}
2298+
throw new TypeError(
2299+
'`jest.advanceTimersToNextFrame()` is not available when using legacy fake timers.',
2300+
);
2301+
},
22922302
advanceTimersToNextTimer: steps =>
22932303
_getFakeTimers().advanceTimersToNextTimer(steps),
22942304
advanceTimersToNextTimerAsync: async steps => {
@@ -2403,16 +2413,6 @@ export default class Runtime {
24032413
);
24042414
}
24052415
},
2406-
runToFrame: () => {
2407-
const fakeTimers = _getFakeTimers();
2408-
2409-
if (fakeTimers === this._environment.fakeTimersModern) {
2410-
return fakeTimers.runToFrame();
2411-
}
2412-
throw new TypeError(
2413-
'`jest.runToFrame()` is not available when using legacy fake timers.',
2414-
);
2415-
},
24162416
setMock: (moduleName, mock) => setMockFactory(moduleName, () => mock),
24172417
setSystemTime: now => {
24182418
const fakeTimers = _getFakeTimers();

packages/jest-types/__typetests__/jest.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,9 @@ expectError(jest.runOnlyPendingTimers(true));
527527
expectType<Promise<void>>(jest.runOnlyPendingTimersAsync());
528528
expectError(jest.runOnlyPendingTimersAsync(true));
529529

530-
expectType<void>(jest.runToFrame());
531-
expectError(jest.runToFrame(true));
532-
expectError(jest.runToFrame(100));
530+
expectType<void>(jest.advanceTimersToNextFrame());
531+
expectError(jest.advanceTimersToNextFrame(true));
532+
expectError(jest.advanceTimersToNextFrame(100));
533533

534534
expectType<void>(jest.setSystemTime());
535535
expectType<void>(jest.setSystemTime(1_483_228_800_000));

0 commit comments

Comments
 (0)