Vitest Cheatsheet
Setup
Use this Vitest reference while you build software engineering projects, review code, or refresh the syntax you reach for most.
Vitest Cheatsheet Overview
Use this Vitest cheatsheet as a fast developer reference for JavaScript and React unit tests: install the runner, choose Node or jsdom, wire scripts, and keep common CLI flags close while coding. If you are building a broader programming cheatsheet stack, pair it with the React cheatsheet, Python cheatsheet, SQL cheatsheet, and Git cheatsheet for framework, language, database, and workflow syntax.
Installation
# npm npm install -D vitest # yarn yarn add -D vitest # pnpm pnpm add -D vitest
For browser-mode or jsdom environments, add the appropriate environment package:
npm install -D @vitest/ui @vitest/coverage-v8 jsdom happy-dom
package.json Scripts
{
"scripts": {
"test": "vitest",
"test:run": "vitest run",
"test:watch": "vitest watch",
"test:ui": "vitest --ui",
"test:coverage":"vitest run --coverage"
}
}Minimal Config (vite.config.ts)
/// <reference types="vitest" /> import { defineConfig } from 'vite' export default defineConfig({ test: { globals: true, environment: 'node', // 'jsdom' | 'happy-dom' | 'edge-runtime' }, })
Dedicated vitest.config.ts
import { defineConfig } from 'vitest/config' export default defineConfig({ test: { globals: true, environment: 'jsdom', setupFiles: ['./src/test/setup.ts'], include: ['src/**/*.{test,spec}.{ts,tsx}'], exclude: ['node_modules', 'dist', 'e2e/**'], }, })
Use
vitest/config(notvite) when you don't need a Vite build config — it avoids pulling in unnecessary plugins.
TypeScript: Global Types Without Importing
Add globals: true in config, then reference the types:
// tsconfig.json { "compilerOptions": { "types": ["vitest/globals"] } }
Or add a triple-slash reference in a vitest.d.ts:
/// <reference types="vitest/globals" />Projects (Monorepo)
Define projects in the root vitest.config.ts under test.projects — glob strings and/or inline configs:
// vitest.config.ts import { defineConfig } from 'vitest/config' export default defineConfig({ test: { projects: [ 'packages/*/vitest.config.ts', { test: { name: 'browser', environment: 'happy-dom', include: ['src/**/*.browser.test.ts'], }, }, ], }, })
Run a specific project:
vitest --project browser
Legacy:
vitest.workspace.ts/defineWorkspacewere deprecated in Vitest 3.2 and removed in Vitest 4 — move the array intotest.projects.
Environment Selection Per File
// @vitest-environment jsdom import { describe, it, expect } from 'vitest' describe('DOM test', () => { it('has a document', () => { expect(document).toBeDefined() }) })
Supported docblock values: node, jsdom, happy-dom, edge-runtime.
CLI Flags Reference
| Flag | Description |
|---|---|
--watch | Re-run on file change (default in dev) |
--run | Single run, no watch (default in CI) |
--ui | Launch the Vitest UI |
--coverage | Collect coverage |
--reporter=verbose | Verbose output |
--reporter=dot | Dot output |
--reporter=json | JSON output to stdout |
--outputFile=out.json | Write reporter output to file |
--testNamePattern=<regex> | Filter tests by name |
--project <name> | Run a specific project |
--shard=1/4 | Run shard 1 of 4 (CI parallelism) |
--pool=forks | Use child processes instead of threads |
--no-file-parallelism | Run test files sequentially |
--bail=1 | Stop after N failures |
--changed | Only run tests for changed files |
--passWithNoTests | Exit 0 when no tests found |