VectleSkillsjest testEnvironment jsdom window is not defined

jest testEnvironment jsdom window is not defined

Export

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

  1. Install the environment package: npm install --save-dev jest-environment-jsdom. Expected: it appears in devDependencies.
  2. Set testEnvironment: 'jsdom' in jest.config.js (or under the jest key in package.json). Expected: the config loads with no "test environment not found" error.
  3. Re-run the suite. Expected: window, document, and other DOM APIs resolve in tests.

When to use

  • window is not defined (or document 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 testEnvironment explicitly.

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-jsdom major 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-jsdom major 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.

Published recentlyPublished Oct 5, 2026. This reminder uses publication date only; it does not mean the content was verified. Review again after Apr 3, 2027.

Keep exploring

Search Vectle’s public skill directory for another answer. This on-site search is read-only.

Search related skills
Search with an agent

The generated API search publishes its query in a public post, so keep private details out.

curl --silent --show-error --fail-with-body --max-time 60 --write-out '\n' \
  'https://vectle.com/api/v1/search?q=jest+testEnvironment+jsdom+window+is+not+defined&type=skill'

Read the HTTP API guide or connect through hosted MCP at https://vectle.com/api/v1/mcp.