Jest test fails with window is not defined
ReferenceError: window is not definedShort answer
Jest runs tests in a node environment by default, and node has no window or document. Set testEnvironment to jsdom in the Jest config, install the jest-environment-jsdom package (it is no longer bundled since Jest 28), or use a docblock to switch only the files that need a browser.
Fix it fast
1Find out which cause you have
Check which environment Jest is using for the failing file.
grep -rn "testEnvironment\|@jest-environment" jest.config.* package.json src/2Match what you see, then jump to its fix
- 1
window or document is not defined in a test that renders or touches the DOMThe default node environment is in useFix - 2
You set testEnvironment to jsdom, but Jest cannot use it, on Jest 28 or newerjest-environment-jsdom is not installedFix - 3
Server-side tests pass in node, and only the component tests failOnly some test files need the browserFix
- 1
What does the error look like?
ReferenceError: window is not definedReferenceError: document is not defined
Cause 1 of 3
The default node environment is in use
1Check
The default testEnvironment is node. No browser globals exist unless you ask for jsdom.
npx jest --showConfig | grep testEnvironment2Fix
Set testEnvironment to jsdom for the project.
// jest.config.js
module.exports = {
testEnvironment: 'jsdom',
};3Verify
Run the failing test again.
npx jest path/to/failing.test.jsConfirmed by jestjs.io
Cause 2 of 3
jest-environment-jsdom is not installed
1Check
From Jest 28 the jsdom environment is no longer part of the default install. It must be added explicitly.
npm ls jest jest-environment-jsdom2Fix
Install it. Use a version that matches your Jest version.
npm install --save-dev jest-environment-jsdom3Verify
The package is listed next to jest, with the same major version.
npm ls jest jest-environment-jsdomConfirmed by jestjs.io
Cause 3 of 3
Only some test files need the browser
1Check
Changing the whole project may slow the node tests or hide mistakes in server code.
grep -rln "document\.\|window\." src/ --include=*.test.*2Fix
Put a docblock at the top of each file that needs the DOM.
/**
* @jest-environment jsdom
*/
test('uses the DOM', () => {
const element = document.createElement('div');
expect(element).not.toBeNull();
});3Verify
Run that file and the node-only files.
npx jestConfirmed by jestjs.io
How do I stop it coming back?
- Decide the test environment per project when you create the Jest config, and write it down in the config.
- Keep DOM code and server code in separate test folders, so each can have its own environment.
- Pin jest and jest-environment-jsdom to the same major version.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix