-
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
Add window to jest setup #28067
Add window to jest setup #28067
Conversation
RNTester (Android/hermes/arm64-v8a): 3289088 bytes |
RNTester.app (iOS): 10657792 bytes |
Thanks for this @timomeh, do you mind rebasing to get these tests to pass and we'll import for review? |
`window` exists in the React Native runtime, but not inside the test environment. Many libraries use `typeof window === 'undefined'` to check if code is running in SSR. Because of the difference in the real environment and test environment, tests can behave different than the real app, which is an unwanted behavior.
Base commit: f536f82 |
@charlesbdudley Done! The test_ios_unit_hermes check is failing, but it also fails on the main branch. |
Base commit: 3560753 |
@charlesbdudley has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
@charlesbdudley merged this pull request in bc1c533. |
Summary
window
exists in the React Native runtime, but not inside the test environment. Many libraries usetypeof window === 'undefined'
to check if code is running in SSR. Because of the difference in the real environment and test environment, tests can behave different than the real app, which is an unwanted behavior.Background
I'm using https://github.com/tannerlinsley/react-query in my React Native Project, which works really well. When writing tests, they wouldn't work: jest started and then seemingly did nothing. While debugging I noticed the render was stuck in an infinite loop. Then I noticed the following line inside
react-query
:I didn't know that the React Native runtime has a global
window
, and thought it's a bug inside react-query. But it does have awindow
, which is not defined inside the test environment.The infinite loop was caused by react-query thinking it is running on the server, which doesn't fetch any data. If the react-query hook mounts, it re-executes because then it should be mounted inside the client. But
isServer
was stilltrue
. This repeats forever.Changelog
[General] [Fixed] - Fix
window
not existing in jest setupTest Plan
Are there tests to check if the test environment is setup correctly? 🤔