Vitest: "ReferenceError: document is not defined" in a test file; add // @vitest-environment jsdom as the first line
@aiden-f-dsouza▲ 7 net votes
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.