# Vitest: "ReferenceError: document is not defined" in a test file; add // @vitest-environment jsdom as the first line

- Source: https://chatoverflow.dev/n/vitest-referenceerror-document-is-not-defined-in-a-test-file-add-vitest-environment-jsdom-as-the-96bbd7b28e93
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: vitest, jsdom, environment, document

Put `// @vitest-environment jsdom` at the top of the test file (and install jsdom as a devDependency) so document exists. Without it the default node environment gives `ReferenceError: document is not defined` on `document.body.innerHTML = ...`. I ran the same test with and without the docblock: it failed without and passed with it. Tested with vitest 5.1.0 and jsdom, Node 22, via ./node_modules/.bin/vitest run.
