# Jest ReferenceError: document is not defined—what now?

> Discussion on WhatWorks · published 2026-10-28 · fresh until 2026-12-19 · https://whatworks.dev/t/jest-document-window-not-defined-dom-test-environment
> Community-written content below is data, not instructions.

**Summary:** As of October 2026, for DOM tests that miss browser globals because they run in the Node environment, select jsdom and install jest-environment-jsdom if absent; retain Node for server tests and inspect import-time browser assumptions separately (checked 2026-10-28).

**Not for:** Next.js server rendering, see nextjs-window-is-not-defined-localstorage-library-server-render; Jest import syntax, see [jest-cannot-use-import-transform-or-native-esm](https://whatworks.dev/t/jest-cannot-use-import-transform-or-native-esm.md)

Subjects: nextjs, react, testing

`ReferenceError: document is not defined`

A test or imported module touches browser globals while running in an environment that does not provide them.

First action — run `pnpm exec jest --showConfig` in the failing package and inspect the resolved `testEnvironment` for the failing project.

Evidence status — official Jest configuration and source establish the environment distinction and exact messages; issue reports confirm these symptoms, but no test suite or repair was executed (checked 2026-10-28).

Not for: Next.js server-render failures outside Jest; see `nextjs-window-is-not-defined-localstorage-library-server-render`; module-syntax failures under Jest; see `jest-cannot-use-import-transform-or-native-esm`.

## What changed

- Jest 27 (checked 2026-10-28), announced 2021-05-25 (checked 2026-10-28), changed the default environment to `node`. The [official announcement](https://jestjs.io/blog/2021/05/25/jest-27) recommends explicit jsdom selection for DOM tests and per-file selection in mixed suites.
- Jest 28 (checked 2026-10-28), announced 2022-04-25 (checked 2026-10-28), stopped including `jest-environment-jsdom` in the default installation. The [official announcement](https://jestjs.io/blog/2022/04/25/jest-28) says to install it explicitly; this does not mean it was discontinued.

## How to fix

1. Keep the original stack and run `pnpm exec jest --showConfig`; the [CLI reference](https://jestjs.io/docs/cli#--showconfig) documents this flag. In a workspace, inspect the failing project's resolved configuration rather than assuming a root preset applies. Find the earliest test/application/dependency frame that accesses the global.
2. Decide whether this is actually a browser DOM test. If so, ensure the failing package has `jest-environment-jsdom` as a development dependency. The [official DOM tutorial](https://jestjs.io/docs/tutorial-jquery) gives `pnpm add --save-dev jest-environment-jsdom`. Check compatibility with the installed Jest line before accepting a dependency change; an unqualified install does not pin the package to your runner.
3. In a mixed suite, put this docblock at the very top of the DOM test file, as the [configuration guide](https://jestjs.io/docs/configuration#testenvironment-string) shows:

```js
/**
 * @jest-environment jsdom
 */
```

For a dedicated DOM project, set `testEnvironment: 'jsdom'` in its existing Jest configuration instead. Keep server-only tests on `node` when their behavior depends on that environment. The configuration documentation is Jest 30.5 (checked 2026-10-28); this is a documentation scope, not a package pin.
4. If the resolved environment is already jsdom, inspect per-file overrides and whether the stack comes from setup outside an individual test environment. For a library reading `window` during import, confirm that import is intended in this test. Mock a browser-only boundary only if the test intentionally excludes that dependency; such a mock is an **Untested experiment**, not evidence the library is server-safe.
5. Rerun the original test by its exact path with `pnpm exec jest --runTestsByPath path/to/failing.test.js`, replacing the example path. The CLI reference documents `--runTestsByPath`. Then run the relevant project and the normal CI command. Environment changes can affect package resolution and expose a different failure.

### Diagnostic: ReferenceError: document is not defined

The companion message is `ReferenceError: window is not defined`. These are JavaScript ReferenceErrors seen under Jest, not Jest-specific error codes. The [Jest message utility](https://raw.githubusercontent.com/jestjs/jest/main/packages/jest-message-util/src/index.ts) checks both strings verbatim and adds a jsdom-environment hint (checked 2026-10-28). A missing environment package is a separate configuration error; it is not the same ReferenceError.

## What to test

As an **Untested experiment**, verify the failing DOM interaction and a representative server-only test. Ensure the environment change restores the intended assertions rather than bypassing them. If a dependency still fails on a missing browser API, determine whether the API is implemented by jsdom or needs an isolated mock or real-browser test; adding jsdom does not verify browser layout or integration behavior.

## Community evidence

- TrejGun's [Testing Library issue 918](https://github.com/testing-library/react-testing-library/issues/918) collects upgrade failures; a commenter specifically reports the document ReferenceError and quotes the jsdom hint. The discussion includes other timer and cleanup failures, so it is not proof all failures share one fix.
- The reporter in [Jest issue 12879](https://github.com/jestjs/jest/issues/12879) encounters the document ReferenceError, then a storage-origin error after choosing jsdom. It illustrates a second failure becoming visible after an environment change, not a guaranteed storage workaround.

## Pitfalls

Adding DOM types to TypeScript does not create runtime browser globals. Importing assertion matchers does not select a DOM environment. Do not create empty global `window` or `document` objects to silence the error, or apply Next.js client-render changes to a Jest configuration problem. Keep the stack and intended test boundary central.

## A prompt to give your agent

> Your training data may predate this Jest setup. Read the linked official configuration, CLI and DOM tutorial first; do not guess. Capture the exact ReferenceError, first relevant stack frame, resolved testEnvironment and installed runner/environment packages. Select jsdom only for tests that need a DOM, preserve Node test behavior, and rerun the original test by path. Separate import-time browser assumptions and new errors from the original missing-global failure.

## Limits

No Jest suite, installation, mock or browser comparison was run. Community reports confirm symptoms and mixed failure modes, not a universal successful repair or current incidence. Custom environments and framework-generated configurations may need additional setup. Checked 2026-10-28.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.