Skip to content

Commit

Permalink
exposing runToFrame from jest runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon committed Oct 4, 2023
1 parent 362c6ed commit ec04c81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,14 @@ export interface Jest {
* 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.
*/
/**
* Advances all timers by the needed milliseconds to execute the next animation frame.
* `runToFrame()` helpful way to execute code that is scheduled using `requestAnimationFrame`.
*
* @remarks
* Not available when using legacy fake timers implementation.
*/
runToFrame(): void;
setMock(moduleName: string, moduleExports: unknown): Jest;
/**
* Set the current system time used by fake timers. Simulates a user changing
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,16 @@ export default class Runtime {
);
}
},
runToFrame: () => {
const fakeTimers = _getFakeTimers();

if (fakeTimers === this._environment.fakeTimersModern) {
return fakeTimers.runToFrame();
}
throw new TypeError(
'`jest.runToFrame()` is not available when using legacy fake timers.',
);
},
setMock: (moduleName, mock) => setMockFactory(moduleName, () => mock),
setSystemTime: now => {
const fakeTimers = _getFakeTimers();
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-types/__typetests__/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@ expectError(jest.runOnlyPendingTimers(true));
expectType<Promise<void>>(jest.runOnlyPendingTimersAsync());
expectError(jest.runOnlyPendingTimersAsync(true));

expectType<void>(jest.runToFrame());
expectError(jest.runToFrame(true));
expectError(jest.runToFrame(100));

expectType<void>(jest.setSystemTime());
expectType<void>(jest.setSystemTime(1_483_228_800_000));
expectType<void>(jest.setSystemTime(Date.now()));
Expand Down

0 comments on commit ec04c81

Please sign in to comment.