Jest test fails with window is not defined

WebpackJestjsYarnpkg

Webpack Problem Overview


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

Webpack Solutions


Solution 1 - Webpack

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.

Solution 2 - Webpack

In your package.json add window like global something like this

"jest": {
  "verbose": true,
  "preset": "react-native",
  "setupFiles": ["./jest/setup.js"],
  "testRegex": "(/tests/.*|\\.(test|spec))\\.(ts|tsx|js)$",
  "transformIgnorePatterns": [
    "node_modules/(?!(jest-)?react-native|lottie-react-native)"
  ],
  "globals": {
    "window": {}
  }
}

Solution 3 - Webpack

2021 (Jest 27)

Jest's testEnvironment default used to be jsdom. It was changed to node recently starting with version 27.

You can set testEnvironment: 'jsdom' in your config file to keep using JSDOM.

However 'node' seems to be a lot faster, so you should be mocking browser APIs where possible.

https://jestjs.io/blog/2021/05/25/jest-27

Solution 4 - Webpack

npm i jest-environment-jsdom

index.test.tsx

/**
 * @jest-environment jsdom
 */

Solution 5 - Webpack

If you want to keep testEnvironment set to node, you can configure a global window in your jest.config.js/ts and then mock what you need in your test cases.

Works fine as long as you don't need any advanced features. Same answer as some of the high rated above but including some examples.

import type {Config} from '@jest/types';

const config: Config.InitialOptions = {
  ...jestConfig,
  testEnvironment: "node",
  globals: {
    window: {
     location: {}
    }
  }
};

export default config;

SomeTestCase.spec.ts

test('Is correct href', () => {
  window.location.href = 'https://somedomain.com';
  expect(window.location.href).toBe('https://somedomain.com');
})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGunnar EketrappView Question on Stackoverflow
Solution 1 - WebpackJacksonHaenchenView Answer on Stackoverflow
Solution 2 - WebpackMiguel CardenasView Answer on Stackoverflow
Solution 3 - Webpackj2L4eView Answer on Stackoverflow
Solution 4 - Webpack小弟调调View Answer on Stackoverflow
Solution 5 - WebpackSoldersView Answer on Stackoverflow