Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

ReferenceError: window is not defined #609

Closed
m-vdb opened this issue Nov 14, 2017 · 11 comments
Closed

ReferenceError: window is not defined #609

m-vdb opened this issue Nov 14, 2017 · 11 comments

Comments

@m-vdb
Copy link

m-vdb commented Nov 14, 2017

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 git@github.com:m-vdb/algolia-window-issue.git
$ cd algolia-window-issue
$ node --version
v6.11.2
$ npm install .
$ npm test
> algolia-with-issue@0.0.1 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.
@Haroenv
Copy link
Contributor

Haroenv commented Nov 14, 2017

Thanks for opening this issue, could you quickly make a repository with this behaviour? We have RN tests with Jest without problem 🤔

@m-vdb
Copy link
Author

m-vdb commented Nov 14, 2017

there you go @Haroenv I've updated the desc

@vvo
Copy link
Contributor

vvo commented Nov 16, 2017

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

@Haroenv
Copy link
Contributor

Haroenv commented Nov 16, 2017

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

@samouss
Copy link
Collaborator

samouss commented Nov 16, 2017

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

@samouss
Copy link
Collaborator

samouss commented Nov 16, 2017

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

@m-vdb
Copy link
Author

m-vdb commented Nov 16, 2017

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

@Haroenv
Copy link
Contributor

Haroenv commented Nov 17, 2017

Window is defined in RN code for some globals

@samouss
Copy link
Collaborator

samouss commented Nov 17, 2017

I'm closing this one since we have a workaround.

Feel free to reopen if you feel this isn't sufficient 😄

@vUdav
Copy link

vUdav commented Jul 12, 2019

I'm use this config on my package.json and it's help for me

"jest": {
  "globals": {
    "window": {}
  }
}

@Haroenv
Copy link
Contributor

Haroenv commented Jul 13, 2019

could you make a reproducible example @vUdav ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants