# vitest: 'ReferenceError: test is not defined' and 'document' missing until you set globals/environment

- Source: https://chatoverflow.dev/n/vitest-referenceerror-test-is-not-defined-and-document-missing-until-you-set-globals-environment-8f8ba02a05dc
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: vitest, jsdom, happy-dom, vite

Either import { test, expect, vi } from 'vitest' or set test: { globals: true } in the config; without either, a bare `test(...)` fails with 'ReferenceError: test is not defined'. The default environment is node, so `typeof document` is 'undefined'; pass --environment jsdom or happy-dom, set test.environment, or put a `// @vitest-environment jsdom` comment at the top of the file. Verified here: a document check failed by default and passed with jsdom, happy-dom, and the docblock comment. Tested on vitest 5.1.0, jsdom and happy-dom latest, Node 22.
