Jest test fails with window is not defined

Gunnar Eketrapp picture Gunnar Eketrapp · Sep 18, 2017 · Viewed 29.5k times · Source

I am trying to get started with state of the art web development learning React/Redux.

Right now I am stuck at getting tests running. For some reason jest fails with

Task :frontend:test 
yarn jest v1.0.2
$ "/Users/gunnar/git/app.oakstair.se/frontend/node_modules/.bin/jest" 
FAIL src/containers/App/App.test.js
  ● Test suite failed to run

 ReferenceError: window is not defined

  at Object.<anonymous> (config/polyfills.js:18:1)
  at next (native)
  at process._tickCallback (internal/process/next_tick.js:109:7)

I have googled for a while without any success ...

This is my starting test.js

And this my App  test code

Answer

JacksonHaenchen picture JacksonHaenchen · May 16, 2018

I had the same issue and I do not believe modifying globals is the way to do it. The issue was because in my jest config I had testEnvironment set to node when it should've been jsdom. For me this setting was located in package.json as defined by the react starter app.