Env: react-native + jest (during test run)
Version: 4.2.0 (latest)
Bug: Current behavior
When running jest I get the following error:
ReferenceError: window is not defined
at Object.<anonymous> (node_modules/algoliasearch/src/reactnative/builds/algoliasearch.reactnative.js:9:13)
at Object.<anonymous> (node_modules/algoliasearch/reactnative.js:4:16)
at Object.<anonymous> (node_modules/react-instantsearch/native.js:17:18)
Bug: Expected behavior
Use global instead here.
How to reproduce
$ git clone [email protected]:m-vdb/algolia-window-issue.git
$ cd algolia-window-issue
$ node --version
v6.11.2
$ npm install .
$ npm test
> [email protected] test /Users/mvdb/algolia-window-issue
> jest __tests__/
FAIL __tests__/super.spec.js
● Test suite failed to run
ReferenceError: window is not defined
at Object.<anonymous> (node_modules/algoliasearch/src/reactnative/builds/algoliasearch.reactnative.js:9:27)
at Object.<anonymous> (node_modules/algoliasearch/reactnative.js:4:16)
at Object.<anonymous> (node_modules/react-instantsearch/native.js:17:18)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 24.297s
Ran all test suites matching /__tests__\//i.
----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files | 0 | 100 | 0 | 0 | |
index.js | 0 | 100 | 0 | 0 | 2 |
----------|----------|----------|----------|----------|----------------|
npm ERR! Test failed. See above for more details.
Thanks for opening this issue, could you quickly make a repository with this behaviour? We have RN tests with Jest without problem 🤔
there you go @Haroenv I've updated the desc
This is linked to the JavaScript client, I bet when running tests with Jest, if you are in the node env instead of jsdom this trigger that
I don't think that's the cause @vvo, since the default is jsdom and there's no other env specified in https://github.com/m-vdb/algolia-window-issue/blob/master/package.json#L42-L66
~Actually I think the issue is related to the new version of React Native.~ In a React Native environment window is defined by the framework. You can find this declaration in InitializeCore. ~But they recently made a change in the Jest setup file. They now mock by default the file InitializeCore (you can see the line here), so the file is not loaded anymore in the test environment. And so the window object remains undefined.~
Edit: It's not related to newer version of React Native actually, window has been available in the test environment until v0.44.3. The import has been drop in v0.45.x (like the comment of expo-jest is saying). The line responsible was this one if you are interested. Below, the import path to InitializeCore:
ReactNative -> ReactNative(Fiber|Stack) -> ReactNativeInjection -> InitializeCore
As a workaround you can define window in your setup file. It makes more sense to define it rather than using global in the client, because window is defined in the React Native environment.
You can take example on the expo-preset:
if (typeof window !== 'object') {
global.window = global;
global.window.navigator = {};
}
https://github.com/expo/jest-expo/blob/master/src/setup.js#L17
that's exactly what I did to circumvent the problem but I don't think it's an acceptable long-term solution. I don't think that window should be used for RN code. I don't know what's the community thoughts though, what do you think?
Edit: wait, window is defined in the RN env? This makes no sense to me
Window is defined in RN code for some globals
I'm closing this one since we have a workaround.
Feel free to reopen if you feel this isn't sufficient 😄
I'm use this config on my package.json and it's help for me
"jest": {
"globals": {
"window": {}
}
}
could you make a reproducible example @vUdav ?
Most helpful comment
As a workaround you can define
windowin yoursetupfile. It makes more sense to define it rather than usingglobalin the client, becausewindowis defined in the React Native environment.You can take example on the
expo-preset:https://github.com/expo/jest-expo/blob/master/src/setup.js#L17