0

I using create-react-app with redux and log4javascript. I have started writing integration tests and running npm run test with --env=node. After I started using node, I am getting window is not defined error.

Is there a way to define window as global object while running jest tests? I have tried setting

global.window = jest.fn(() => {
  location: { search: '' }
})

but still not working.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Saravanan S
  • 1,021
  • 1
  • 10
  • 24
  • take a look at https://stackoverflow.com/questions/41885841/how-to-mock-the-javascript-window-object-using-jest – Rishikesh Dhokare Jan 02 '19 at 06:55
  • Is there a reason you are using `--env=node`? The default test environment for `Jest` is `jsdom` which provides a browser environment including the `window` object. The `node` environment is for apps designed to run on `node` (server code, etc.). It sounds like you are testing a React app, that should be tested in the `jsdom` environment. – Brian Adams Jan 07 '19 at 03:51
  • I actually have little confusion on that as well. I think `jsdom` is the way to go. But facing some other issues related to my app. – Saravanan S Jan 07 '19 at 04:56

0 Answers0