jest testEnvironment jsdom window is not defined
Fixes the Jest ReferenceError window is not defined by installing jest-environment-jsdom and setting testEnvironment to jsdom, the opt-in required since Jest 28 changed the default to node. Use it when DOM globals are missing in Jest 28 or later. Not for bundler or browser runtime errors, or for jsdom configuration quirks.
TL;DR
Since Jest 28 the default test environment is node, not jsdom, so window does not exist unless you opt in. Install jest-environment-jsdom and set testEnvironment: 'jsdom' in the Jest config. That one change fixes every window is not defined failure at once.
Error
ReferenceError: window is not defined
at Object.[anonymous] (src/components/Button.test.js:3:1)Steps
- Install the environment package:
npm install --save-dev jest-environment-jsdom. Expected: it appears in devDependencies. - Set
testEnvironment: 'jsdom'injest.config.js(or under thejestkey in package.json). Expected: the config loads with no "test environment not found" error. - Re-run the suite. Expected:
window,document, and other DOM APIs resolve in tests.
When to use
window is not defined(ordocument is not defined) after upgrading to Jest 28+.- A fresh Jest install where component tests touch the DOM.
- You relied on the old default and never set
testEnvironmentexplicitly.
When not to use
- The error comes from a bundler or a real browser runtime, not Jest.
- Fine-tuning jsdom behavior (url, pretendToBeVisual) once the environment loads.
- You are on Jest 27 or earlier (jsdom was the default there).
Tool compatibility
- Jest 28, 29, and 30 with a matching
jest-environment-jsdommajor version. - Jest 27 and earlier need no change (jsdom was the default).
Variant phrasings
jest window is not defined after upgrading to jest 28
The upgrade changed the default environment; this is the fix.
ReferenceError document is not defined jest
Same cause and same fix; document comes from the same jsdom environment.
Why it happens
Jest 27 and earlier defaulted to jsdom. Jest 28 flipped the default to node for speed and split jsdom into a separate package you must install. Tests written against the old default break with window is not defined until you opt back in.
Edge cases
- Some tests need node (API tests): set the environment per file with a docblock pragma instead of globally.
- Still fails after the fix: you may have two Jest versions installed; check which config the runner actually loads.
- The
jest-environment-jsdommajor version must match your Jest major version.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_W5wHBlHeVOziP8l4Y-dsQw
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.