Test Vitest Config
Configure Vitest with workspaces, environments, coverage, and TypeScript integration
When to Use
- Setting up Vitest in a new or existing project
- Configuring test environments (node, jsdom, happy-dom)
- Setting up workspaces for monorepo testing
- Integrating coverage, globals, and TypeScript paths
Instructions
- Basic configuration:
// vitest.config.ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['src/**/*.test.ts'],
exclude: ['node_modules', 'dist', 'e2e'],
setupFiles: ['./test/setup.ts'],
testTimeout: 10_000,
},
});
- Share config with Vite (if already using Vite):
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['./test/setup.ts'],
},
});
- Environment per file using comments or config:
// At the top of a test file:
// @vitest-environment jsdom
// Or in config, per glob pattern:
test: {
environmentMatchGlobs: [
['src/components/**', 'jsdom'],
['src/services/**', 'node'],
],
},
- Workspace configuration for monorepos:
// vitest.workspace.ts
export default [
'packages/*/vitest.config.ts',
// Or inline:
{
test: {
name: 'unit',
include: ['src/**/*.test.ts'],
environment: 'node',
},
},
{
test: {
name: 'components',
include: ['src/**/*.test.tsx'],
environment: 'jsdom',
},
},
];
- Coverage configuration:
test: {
coverage: {
provider: 'v8',
reporter: ['text', 'html', 'lcov'],
include: ['src/**/*.ts'],
exclude: ['src/**/*.test.ts', 'src/**/*.d.ts'],
thresholds: {
branches: 80,
functions: 80,
lines: 80,
},
},
},
- Path aliases — sync with tsconfig:
import { defineConfig } from 'vitest/config';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [tsconfigPaths()],
test: {
// ...
},
});
- Setup files for global test configuration:
// test/setup.ts
import '@testing-library/jest-dom';
import { cleanup } from '@testing-library/react';
import { afterEach } from 'vitest';
afterEach(() => {
cleanup();
});
- Type support — add vitest types to tsconfig:
{
"compilerOptions": {
"types": ["vitest/globals"]
}
}
- Parallel execution and pooling:
test: {
pool: 'forks', // 'threads' | 'forks' | 'vmThreads'
poolOptions: {
forks: { maxForks: 4 },
},
fileParallelism: true, // Run test files in parallel
},
- Snapshot configuration:
test: {
snapshotFormat: {
printBasicPrototype: false,
},
resolveSnapshotPath: (testPath, snapExtension) =>
testPath.replace('src/', '__snapshots__/') + snapExtension,
},
Details
Vitest is a Vite-native test framework that shares Vite's configuration, plugins, and transform pipeline. This means your tests use the same module resolution, path aliases, and transforms as your application code.
Environment options:
node— Node.js runtime. For services, utilities, API testsjsdom— Browser-like DOM via jsdom. For React/Svelte/Vue component testshappy-dom— Faster alternative to jsdom with more Web API supportedge-runtime— Cloudflare Workers/Vercel Edge runtime simulation
globals: true makes describe, it, expect, vi available without imports. Cleaner test files but requires TypeScript types configuration.
Pool options:
threads(default) — worker threads, shared memory, fastest for CPU-bound testsforks— child processes, full isolation, best for tests with global state leaksvmThreads— VM contexts, lighter than forks, good middle ground
Performance tips:
- Use
pool: 'threads'for fastest execution - Set
fileParallelism: trueto run files in parallel - Use
--reporter=dotin CI for minimal output - Exclude unnecessary files from test discovery
- Use
--changedflag to run only tests affected by changed files
Source
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.