From b44fe4deee505382698a98d2573691303b0159c3 Mon Sep 17 00:00:00 2001 From: Krzysztof Magiera Date: Mon, 23 Jan 2023 09:53:01 -0800 Subject: [PATCH] Provide timestamp as argument to rAF callbacks when running Jest tests (#35919) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This change aligns requestAnimationFrame implementation used in Jest environment with web standard, and with the implementation that runs in the application environment. As per specification https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame#parameters – requestAnimationFrame callback gets a single parameter, which represents the current frame timestamp. The current polyfill maps requestAnimationFrame directly to setTimeout which makes the callback execute without any parameters. ## Changelog [General] [Fixed] - Jest mocked requestAnimationFrame callbacks now receive a timestamp parameter Pull Request resolved: https://github.com/facebook/react-native/pull/35919 Test Plan: 1. execute jest test suite to make sure nothing breaks 2. add the below code to one of the tests: ``` jest.useFakeTimers(); requestAnimationFrame((timestamp) => console.log("rAF", timestamp)); jest.runOnlyPendingTimers(); jest.useRealTimers(); ``` this code will print `undefined` before and numer `0` representing the mocked frame time after this change. Reviewed By: jacdebug Differential Revision: D42676544 Pulled By: robhogan fbshipit-source-id: 363dc506ccc4bd034408fbb35ad3151875a8d309 --- jest/setup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jest/setup.js b/jest/setup.js index 3112707451595c..71f49e6892f230 100644 --- a/jest/setup.js +++ b/jest/setup.js @@ -25,7 +25,7 @@ global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime'); global.window = global; global.requestAnimationFrame = function (callback) { - return setTimeout(callback, 0); + return setTimeout(() => callback(jest.now()), 0); }; global.cancelAnimationFrame = function (id) { clearTimeout(id);