
Most Vitest problems fall into a small number of categories: the runtime is too old, a peer dependency does not match, the environment lacks a DOM, the watcher is misunderstood as a hang, or a default changed in a new major. This guide lists the errors developers hit most often, explains what each one means, and gives a direct fix. It is written for Vitest 5 and notes where Vitest 4 behaved differently.
Unsupported engine or immediate exit
Symptom: npm prints an EBADENGINE warning during install, or vitest exits without output. Cause: Vitest 5 requires Node.js 22.12 or newer. Fix: run node -v, then switch to a supported version with nvm, fnm, Volta or a fresh installer. In CI, update the Node version in the workflow file or the Docker base image. Keep .nvmrc or the engines field in package.json in sync so teammates get the same version.
Peer dependency conflict on vite
Symptom: npm reports ERESOLVE and mentions vite during install. Cause: Vitest 5 lists Vite 6.4, 7 or 8 as a required peer; an older Vite pin in the project conflicts. Fix: upgrade Vite to a supported version, or install the Vitest major that matches the Vite you must keep. Avoid the legacy-peer-deps flag as a permanent solution because mismatched versions produce confusing runtime errors later.
document is not defined
Symptom: a ReferenceError for document, window or localStorage. Cause: tests run in a Node environment unless told otherwise. Fix: install jsdom or happy-dom as a dev dependency and set test.environment in the config, or add a comment at the top of the file that selects the environment for that file only. For framework components, also add the framework's Vite plugin so templates and JSX compile.
Tests hang or never finish
Symptom: the command seems stuck after the last test. Cause one: running vitest without arguments starts watch mode, which waits for changes by design. Fix: use vitest run for a single pass, especially in CI. Cause two: an open handle such as an HTTP server, database connection or interval keeps the process alive. Fix: close it in afterAll, or use the hanging-process reporter to see what is still open.
Mock counts are wrong after upgrading
Symptom: toHaveBeenCalledTimes fails with a lower number than before. Cause: Vitest 5 sets clearMocks to true, clearing call history before each test; Vitest 4 accumulated calls. Fix: assert within the test that made the calls, or set clearMocks to false in the config while you restructure.
Test fails on an assertion that used to warn
Symptom: a test that passed in Vitest 4 fails with a message about an unawaited assertion. Cause: resolves, rejects and toMatchFileSnapshot must be awaited, and Vitest 5 fails instead of warning. Fix: add await in front of the expect call and make the test function async.
vi.mock factory cannot access a variable
Symptom: a ReferenceError inside a vi.mock factory, or the mock does not apply. Cause: vi.mock is hoisted above imports, so variables declared later are not initialised when the factory runs. Fix: define the values with vi.hoisted and reference them from the factory, or move the mock setup into a setup file.
Cannot find module or alias not resolved
Symptom: an import with an alias such as @ or a workspace package fails to resolve. Cause: aliases are defined in tsconfig paths or a framework config that Vitest is not reading. Fix: add the alias under resolve.alias in the Vite config, or use the vite-tsconfig-paths plugin so tsconfig paths are honoured. In monorepos, define projects so each package resolves against its own root.
Snapshot mismatch after an update
Symptom: snapshots fail after upgrading Vitest or a UI library. Cause: serialisation or component output changed. Fix: review the diff; if the new output is correct, run vitest with the -u flag to update snapshots and commit them. Never update blindly, because the mismatch may be a real regression.
Locator matches nothing in browser mode
Symptom: getByText or getByRole fails in Vitest 5 but worked in 4. Cause: locators are now exact by default, so partial matches no longer succeed. Fix: pass the full text, use a regular expression, or set locators.exact to false if the old behaviour is required. The error output now includes an ARIA snapshot that shows what is actually on the page.
bench is not exported
Symptom: importing bench from vitest fails after upgrading. Cause: in Vitest 5 bench is a fixture on the test context, not a top-level export. Fix: write a regular test and destructure bench from the context argument, then call it inside the test body.
Reports missing in CI
Symptom: the pipeline cannot find junit or html output after upgrading. Cause: Vitest 5 writes these to the .vitest directory. Fix: update the artefact path, add .vitest to .gitignore, and consider the html reporter's singleFile option for easy uploads.
Out of memory on large suites
Symptom: workers crash with heap errors. Cause: too many files per worker or a leak between tests. Fix: try vitest doctor to compare pool settings, enable isolation if it was disabled, split the suite with shards, and check for global state that grows across tests. The v8 coverage provider in Vitest 5 merges with bounded memory, which helps on coverage runs.
Stale results after config changes
Symptom: a changed option seems ignored. Cause: the watcher was started before the config change, or the on-disk module cache holds old transforms. Fix: restart the watcher; if results still look stale, delete the .vitest directory and rerun.
Getting more information
When an error message is unclear, rerun with the verbose reporter to see each test name, or open the Vitest UI to read console output next to the failing test. The hanging-process reporter lists open handles when a run will not exit. For transform problems, the module graph view in the UI shows which file pulled in an unexpected dependency. Record the command and environment before comparing results across machines.
Conclusion
Nearly every Vitest error traces back to the runtime version, a peer dependency, the test environment, watch mode semantics or a changed default. Checking Node and Vite versions first, setting the environment deliberately, using vitest run in CI, and reading the Vitest 5 migration notes for clearMocks, unawaited assertions, strict locators, bench and the .vitest folder will resolve the large majority of failures. When something remains unexplained, the Vitest UI and the hanging-process reporter show exactly what the run is doing.

