Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide timestamp as argument to rAF callbacks when running Jest tests #35919

Closed
wants to merge 2 commits into from

Conversation

kmagiera
Copy link
Contributor

@kmagiera kmagiera commented Jan 21, 2023

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

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.

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Software Mansion Partner: Software Mansion Partner labels Jan 21, 2023
@robhogan
Copy link
Contributor

Thanks for this - completely agree with adding this argument, but IIRC the value should be provided by jest.now(), which should work on both legacy and modern timers independently of selective faking (doNotFake).

@analysis-bot
Copy link

analysis-bot commented Jan 21, 2023

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,461,869 +182
android hermes armeabi-v7a 7,782,462 +196
android hermes x86 8,935,119 +158
android hermes x86_64 8,793,299 +278
android jsc arm64-v8a 9,648,088 +183
android jsc armeabi-v7a 8,382,593 +193
android jsc x86 9,710,324 +168
android jsc x86_64 10,187,446 +282

Base commit: a69a924
Branch: main

@kmagiera
Copy link
Contributor Author

Thanks for checking this @robhogan. Your suggestion makes sense. I was only testing it with modern timers where performance.now is mocked. Will update the PR after the weekend

jest/setup.js Outdated Show resolved Hide resolved
@facebook-github-bot
Copy link
Contributor

@robhogan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Jan 23, 2023
@facebook-github-bot
Copy link
Contributor

@robhogan merged this pull request in b44fe4d.

OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
facebook#35919)

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: facebook#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. p: Software Mansion Partner: Software Mansion Partner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants