react-native-macos/jest
Timo Mämecke bc1c533833 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: https://github.com/facebook/react-native/pull/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
2021-08-24 09:42:17 -07:00
..
MockNativeMethods.js change jest native method mocks to jest functions (#24337) 2019-04-07 11:54:48 -07:00
assetFileTransformer.js Remove `fbjs-scripts` in favor of `@jest/create-cache-key-function` 2020-10-06 18:22:58 -07:00
mockComponent.js Correctly mock all components by setting the displayName 2020-07-28 13:09:06 -07:00
mockNativeComponent.js RN: Create `NativeComponentRegistry` 2020-11-19 10:55:50 -08:00
mockScrollView.js Make ScrollView use ForwardRef 2020-03-26 16:53:46 -07:00
preprocessor.js Fix inline requires for ESM in react-native-github 2021-06-04 13:05:11 -07:00
renderer.js chore: remove FlowFixMe (#29468) 2021-08-04 12:20:15 -07:00
setup.js Add window to jest setup (#28067) 2021-08-24 09:42:17 -07:00