-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Jest setup: Stop polyfilling Promise. #29754
Conversation
It looks like this line was introduced in 3ff3987, in 2015, and it has remained in a similar form since then. I haven't found any explanation for it. At jestjs/jest#10221 [1], a core Jest maintainer says, """ As an aside, one should never replace `global.Promise` [...]. E.g. when using `async-await` you will always get the native `Promise` regardless of the value of `global.Promise`. """ jestjs/jest#10221 is one issue this line has raised, for anyone using the latest features of Jest to test async code in their React Native projects. [1] jestjs/jest#10221 (comment) Fixes: facebook#29303
Hi @chrisbobbe! Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
Base commit: 6d58490 |
Base commit: 6d58490 |
I really hoped this would fix callstack/react-native-testing-library#379 (comment) but after making this change locally everything got broken:
via this hook (from const {colors} = useTheme(); The error does not appear without this change, so it seems there's a legitimate reason for Similar breakage has also been reported here: callstack/react-native-testing-library#379 (comment) |
Hmm, I don't see anything there related to
I'm not sure how legitimate the reason is; possibly the choice wasn't well-considered, and development continued in a way that wasn't compatible with the native |
There are a bunch of failing tests which seem to hint at some functionality related to pausing promises. This probably can't be done with native promises, hence why a polyfill was used. |
Yeah, possibly. That would be a good question to answer: can the code be changed so that it works with native The choice affects React Native's public API; app developers are encouraged to use React Native's Jest preset, which currently activates the polyfill when the developer tests their own app (as we see with jestjs/jest#10221). The choice to restrict app developers to use a particular, non-standard Also, one reason not to replace the global
If the polyfill is sometimes used and sometimes not, depending on what syntactic sugar happens to be used, that seems like a likely cause of some pretty hard-to-track bugs—and an obstacle to writing stable async code in a sane way. If React Native has a workaround for that, I'd appreciate documentation for it too. 🙂 |
Just wanted to add some information that I encountered while trying to override the Example: react-native/Libraries/Interaction/InteractionManager.js Lines 115 to 124 in 1465c8f
which causes issues if you are trying to use
Due to a chained react-native/Libraries/Interaction/TaskQueue.js Lines 162 to 178 in 1465c8f
|
Hey @chrisbobbe - sorry I didn't see this PR before creating #34659 . There were some tests that needed updating to work with native Promises, but ultimately I think you're right that this was unnecessary and needed removing. |
@robhogan I don't want to cause panic here, but I vaguely remember trying to patch RN in the past to use the native promise, and it broke various things that I don't remember right now. :) It could've affected other libraries, like react-native-testing-library, but again, I forgot the specifics. There are some assumptions in certain places that the polyfilled promise is being used. See the comment above: #29754 (comment) (although, I see that that was changed by now) Fingers crossed that #34659 isn't a big breaking change. :) |
Hi @andreialecu - thanks for the heads up. I patched up our internal tests, but you're right that there could be a knock-on in This is part of an effort to update Jest, where modern timers in recent versions have better Promise handling, so I hope if there are issues created by this, modern Jest might be a way out. @thymikee, @mdjastrzebski - just FYI, let me know if you foresee an issue here. |
@robhogan we already use node's Promise in our preset which we recommend to our users: https://github.com/callstack/react-native-testing-library/blob/main/jest-preset/index.js |
Hi! 👋 I hope anyone reading this is doing OK these days. Thanks, in any case, for continuing to put your time into the awesome React Native ecosystem.
Summary
It looks like this line was introduced in 3ff3987, in 2015, and it
has remained in a similar form since then. I haven't found any
explanation for it.
At jestjs/jest#10221 [1], a core Jest maintainer says,
"""
As an aside, one should never replace
global.Promise
[...]. E.g.when using
async-await
you will always get the nativePromise
regardless of the value of
global.Promise
."""
jestjs/jest#10221 is one issue this line has raised, for anyone
using the latest features of Jest to test async code in their React
Native projects.
[1] jestjs/jest#10221 (comment)
Fixes: #29303
Changelog
[General] [Fixed] - Fix issue with testing async code with Jest's fake timers.
Test Plan
It looks like there's still an effort to keep up-to-date with new versions of the
promise
NPM package; see b23efc5. But that commit still leaves it a mystery why we're polyfillingPromise
here.I also see Libraries/Core/polyfillPromise.js, but I haven't run into any problems that I can trace to that file.
It's hard to say what side effects might arise from removing the line without knowing what it's for. (#29303 exists mostly to ask that question.)