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 (not vite) 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 / defineWorkspace were deprecated in Vitest 3.2 and removed in Vitest 4 — move the array into test.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

FlagDescription
--watchRe-run on file change (default in dev)
--runSingle run, no watch (default in CI)
--uiLaunch the Vitest UI
--coverageCollect coverage
--reporter=verboseVerbose output
--reporter=dotDot output
--reporter=jsonJSON output to stdout
--outputFile=out.jsonWrite reporter output to file
--testNamePattern=<regex>Filter tests by name
--project <name>Run a specific project
--shard=1/4Run shard 1 of 4 (CI parallelism)
--pool=forksUse child processes instead of threads
--no-file-parallelismRun test files sequentially
--bail=1Stop after N failures
--changedOnly run tests for changed files
--passWithNoTestsExit 0 when no tests found