diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 6a99b3c7fe39..29b8b0cf7e2f 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -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 diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 1057ee8a0d84..2f9ff437428b 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -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(); diff --git a/packages/jest-types/__typetests__/jest.test.ts b/packages/jest-types/__typetests__/jest.test.ts index 7a513623bd87..b8142da12106 100644 --- a/packages/jest-types/__typetests__/jest.test.ts +++ b/packages/jest-types/__typetests__/jest.test.ts @@ -527,6 +527,10 @@ expectError(jest.runOnlyPendingTimers(true)); expectType>(jest.runOnlyPendingTimersAsync()); expectError(jest.runOnlyPendingTimersAsync(true)); +expectType(jest.runToFrame()); +expectError(jest.runToFrame(true)); +expectError(jest.runToFrame(100)); + expectType(jest.setSystemTime()); expectType(jest.setSystemTime(1_483_228_800_000)); expectType(jest.setSystemTime(Date.now()));