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// Test timeout in ms77testTimeout: 5000,7879// Hook timeout in ms80hookTimeout: 10000,8182// Enable watch mode by default83watch: true,8485// Coverage configuration86coverage: {87provider: 'v8', // or 'istanbul'88reporter: ['text', 'html'],89include: ['src/**/*.ts'],90},9192// Run tests in isolation (each file in separate process)93isolate: true,9495// Pool for running tests: 'threads', 'forks', 'vmThreads'96pool: 'threads',9798// Number of threads/processes99poolOptions: {100threads: {101maxThreads: 4,102minThreads: 1,103},104},105106// Automatically clear mocks between tests107clearMocks: true,108109// Restore mocks between tests110restoreMocks: true,111112// Retry failed tests113retry: 0,114115// Stop after first failure116bail: 0,117},118})119```120121## Conditional Configuration122123Use `mode` or `process.env.VITEST` for test-specific config:124125```ts126export default defineConfig(({ mode }) => ({127plugins: mode === 'test' ? [] : [myPlugin()],128test: {129// test options130},131}))132```133134## Projects (Monorepos)135136Run different configurations in the same Vitest process:137138```ts139defineConfig({140test: {141projects: [142'packages/*',143{144test: {145name: 'unit',146include: ['tests/unit/**/*.test.ts'],147environment: 'node',148},149},150{151test: {152name: 'integration',153include: ['tests/integration/**/*.test.ts'],154environment: 'jsdom',155},156},157],158},159})160```161162## Key Points163164- Vitest uses Vite's transformation pipeline - same `resolve.alias`, plugins work165- `vitest.config.ts` takes priority over `vite.config.ts`166- Use `--config` flag to specify a custom config path167- `process.env.VITEST` is set to `true` when running tests168- Test config uses `test` property, rest is Vite config169170<!--171Source references:172- https://vitest.dev/guide/#configuring-vitest173- https://vitest.dev/config/174-->175