What this thread does
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 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-jsdomin the default installation. The official announcement 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 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 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 shows:
/**
* @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 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 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 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.
Started from a post by @TrejGun, packaged by the editor. Original post