Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vitest 3.x reference skill covering configuration, test/describe APIs, mocking, coverage, snapshots, and concurrency.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/core-config.md
1---2name: vitest-configuration3description: Configure Vitest with vite.config.ts or vitest.config.ts4---56# Configuration78Vitest reads configuration from `vitest.config.ts` or `vite.config.ts`. It shares the same config format as Vite.910## Basic Setup1112```ts13// vitest.config.ts14import { defineConfig } from 'vitest/config'1516export default defineConfig({17test: {18// test options19},20})21```2223## Using with Existing Vite Config2425Add Vitest types reference and use the `test` property:2627```ts28// vite.config.ts29/// <reference types="vitest/config" />30import { defineConfig } from 'vite'3132export default defineConfig({33test: {34globals: true,35environment: 'jsdom',36},37})38```3940## Merging Configs4142If you have separate config files, use `mergeConfig`:4344```ts45// vitest.config.ts46import { defineConfig, mergeConfig } from 'vitest/config'47import viteConfig from './vite.config'4849export default mergeConfig(viteConfig, defineConfig({50test: {51environment: 'jsdom',52},53}))54```5556## Common Options5758```ts59defineConfig({60test: {61// Enable global APIs (describe, it, expect) without imports62globals: true,6364// Test environment: 'node', 'jsdom', 'happy-dom'65environment: 'node',6667// Setup files to run before each test file68setupFiles: ['./tests/setup.ts'],6970// Include patterns for test files71include: ['**/*.{test,spec}.{js,ts,jsx,tsx}'],7273// Exclude patterns74exclude: ['**/node_modules/**', '**/dist/**'],7576// Limit test discovery to a directory (faster than broad excludes)77dir: './src',7879// Test timeout in ms80testTimeout: 5000,8182// Hook timeout in ms83hookTimeout: 10000,8485// Coverage configuration (v4+: define `include`, no more `all`)86coverage: {87provider: 'v8', // or 'istanbul'88reporter: ['text', 'html'],89include: ['src/**/*.ts'],90},9192// Run each file in an isolated module graph (threads/forks pools only)93isolate: true,9495// Pool: 'forks' (default), 'threads', 'vmForks', 'vmThreads'96pool: 'forks',9798// v4+: pool options are top-level (poolOptions was removed)99maxWorkers: 4,100fileParallelism: true,101102// Automatically clear mocks between tests103clearMocks: true,104105// Restore spies created with vi.spyOn between tests106restoreMocks: true,107108// Retry failed tests109retry: 0,110111// Stop after first failure112bail: 0,113},114})115```116117## v4/v5 Config Changes118119- **Pool default is `forks`** (child processes), not `threads`.120- **`poolOptions` removed** — `maxThreads`/`maxForks` are now top-level `maxWorkers`; `singleThread`/`singleFork` become `maxWorkers: 1, isolate: false`; VM `memoryLimit` is `vmMemoryLimit`. `minWorkers` was removed.121- **`workspace` removed** — use [`projects`](advanced-projects.md). `vitest.workspace.ts` no longer supported.122- **`coverage.all` and `coverage.extensions` removed** — by default only covered files are reported; set `coverage.include` explicitly.123- **Simplified `exclude`** — only `node_modules`/`.git` excluded by default. Use `test.dir` to scope discovery, or spread `configDefaults.exclude`.124- **Config not looked up from parent dirs** — pass `--config` explicitly when running from a subdirectory.125- **`.vitest` artifact dir** — blob reports (`.vitest/blob/`), attachments (`.vitest/attachments/`), and HTML report now live under a single `.vitest/` directory; add one entry to `.gitignore`.126- `deps.optimizer.web` renamed to `deps.optimizer.client`; `deps.inline`/`deps.external` moved under `server.deps`.127128## Conditional Configuration129130Use `mode` or `process.env.VITEST` for test-specific config:131132```ts133export default defineConfig(({ mode }) => ({134plugins: mode === 'test' ? [] : [myPlugin()],135test: {136// test options137},138}))139```140141## Projects (Monorepos)142143Run different configurations in the same Vitest process:144145```ts146defineConfig({147test: {148projects: [149'packages/*',150{151test: {152name: 'unit',153include: ['tests/unit/**/*.test.ts'],154environment: 'node',155},156},157{158test: {159name: 'integration',160include: ['tests/integration/**/*.test.ts'],161environment: 'jsdom',162},163},164],165},166})167```168169## Key Points170171- Vitest uses Vite's transformation pipeline - same `resolve.alias`, plugins work172- `vitest.config.ts` takes priority over `vite.config.ts`173- Use `--config` flag to specify a custom config path (required from subdirectories in v5)174- `process.env.VITEST` is set to `true` when running tests175- Test config uses `test` property, rest is Vite config176- v4 requires **Vite >= 6** and **Node >= 20**; v5 is currently in beta177178<!--179Source references:180- https://vitest.dev/guide/#configuring-vitest181- https://vitest.dev/config/182-->183