Skip to content

Commit bc1c533

Browse files
timomehfacebook-github-bot
authored andcommitted
Add window to jest setup (#28067)
Summary: `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. ## 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`: ```js const isServer = typeof window === 'undefined' ``` 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 a `window`, 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 still `true`. This repeats forever. ## Changelog [General] [Fixed] - Fix `window` not existing in jest setup Pull Request resolved: #28067 Test Plan: Are there tests to check if the test environment is setup correctly? � Reviewed By: yungsters Differential Revision: D30317021 Pulled By: charlesbdudley fbshipit-source-id: 837ed952833ef8e70c5132c9b4152b0e0f28b4dd
1 parent ddf9a63 commit bc1c533

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

jest/setup.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ global.performance = {
2323

2424
global.Promise = jest.requireActual('promise');
2525
global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime');
26+
global.window = global;
2627

2728
global.requestAnimationFrame = function(callback) {
2829
return setTimeout(callback, 0);

0 commit comments

Comments
 (0)