Sub topic 14
Choosing a Test Environment: node, jsdom, or happy-dom
This chapter assumes you can write tests with test and expect, use beforeEach, and edit a vitest.config.js file as shown in the previous chapter.
A test environment is the set of global objects your test code can use while it runs. Vitest runs tests in Node.js. Node.js has process and Buffer, but it has no window, document, or HTMLElement. If your code touches the DOM, you need an environment that simulates a browser.
The Built-in Choices
| Environment | What it provides | Install | Typical use |
|---|---|---|---|
| `node` (default) | Node.js globals only | Nothing | Pure logic, servers, CLIs |
| `jsdom` | A full DOM simulation | `npm install -D jsdom` | UI code that needs wide web API coverage |
| `happy-dom` | A lighter, faster DOM simulation | `npm install -D happy-dom` | UI code where speed matters more than full API coverage |
Vitest also ships an edge-runtime environment for code that targets edge functions. This chapter covers the three you will use most.
Setting the Environment
To set one environment for the whole project, use the environment option in vitest.config.js. To change it for a single file, put a comment at the top of that file. The comment overrides the config, so a mostly-Node project can opt in to a DOM only where it needs one.
// @vitest-environment jsdom
import { test, expect, beforeEach } from 'vitest'
function renderGreeting(container, name) {
const p = document.createElement('p')
p.textContent = `Hello, ${name}`
container.appendChild(p)
}
beforeEach(() => {
document.body.innerHTML = '<div id="app"></div>'
})
test('renders a greeting', () => {
const app = document.getElementById('app')
renderGreeting(app, 'Ada')
expect(app.querySelector('p').textContent).toBe('Hello, Ada')
})
test('starts each test with an empty container', () => {
const app = document.getElementById('app')
expect(app.children.length).toBe(0)
})To run the same file on happy-dom, change the comment to // @vitest-environment happy-dom. The test body does not change.
Common Errors
ReferenceError: document is not defined: the file runs innode. Add the environment comment or change the config.- Missing package error at startup: you selected
jsdomorhappy-domwithout installing it. Vitest does not bundle either package. - Layout values are always zero: neither simulator renders pages.
getBoundingClientRect()returns zeros andoffsetWidthis0. Test layout in a real browser, for example with Vitest's browser mode or an end-to-end tool. - Different results between simulators: happy-dom and jsdom implement some APIs differently. If a test passes in one and fails in the other, check whether your code depends on behavior one of them doesn't support yet.
- Slow suites: setting
jsdomglobally makes every file pay its startup cost. Keepnodeas the default and opt in per file.
Exercise
Your project's config leaves the environment at its default. A new file tests a function that calls document.querySelector, and the run fails with "document is not defined". Give the smallest change that fixes only this file, and say what else must be true for it to work.
Show answer
Add // @vitest-environment jsdom (or happy-dom) as the first line of that test file. The chosen package must also be installed as a dev dependency, for example with npm install -D jsdom. Other files keep running in node.
Exercise
Two tests in the same jsdom file both append a <li> to document.body and assert there is exactly one <li>. The second test fails. Why, and how do you fix it?
Show answer
Tests in one file share the same document, so the first test's <li> is still there when the second test runs. Reset the DOM in a beforeEach, for example with document.body.innerHTML = ''.