← スキル一覧に戻る

vitest-configuration
by IvanTorresEdge
⭐ 0🍴 1📅 2026年1月13日
SKILL.md
name: vitest-configuration description: Vitest setup and best practices. Use when configuring Vitest for TypeScript projects.
Vitest Configuration Skill
This skill covers Vitest configuration for TypeScript projects.
When to Use
Use this skill when:
- Setting up testing infrastructure
- Configuring coverage thresholds
- Setting up test environments
- Migrating from Jest to Vitest
Core Principle
FAST, NATIVE ESM TESTING - Vitest provides Jest-compatible API with native ESM support and Vite-powered performance.
Installation
npm install -D vitest @vitest/coverage-v8
Basic Configuration
vitest.config.ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['src/**/__tests__/**/*.test.{ts,tsx}'],
exclude: ['node_modules', 'dist'],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
exclude: [
'node_modules/',
'dist/',
'**/*.test.ts',
'**/__tests__/**',
'**/*.d.ts',
],
thresholds: {
lines: 80,
functions: 80,
branches: 80,
statements: 80,
},
},
typecheck: {
enabled: true,
tsconfig: './tsconfig.json',
},
},
});
Package.json Scripts
{
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage",
"test:ui": "vitest --ui"
}
}
Test Environments
Node Environment (Default)
export default defineConfig({
test: {
environment: 'node',
},
});
JSDOM Environment (For DOM Testing)
npm install -D jsdom
export default defineConfig({
test: {
environment: 'jsdom',
},
});
Happy-DOM Environment (Faster Alternative)
npm install -D happy-dom
export default defineConfig({
test: {
environment: 'happy-dom',
},
});
Per-File Environment
// In test file header
// @vitest-environment jsdom
import { describe, it, expect } from 'vitest';
Global Setup and Teardown
Setup Files
// vitest.config.ts
export default defineConfig({
test: {
setupFiles: ['./src/test/setup.ts'],
globalSetup: ['./src/test/global-setup.ts'],
},
});
// src/test/setup.ts
import { beforeAll, afterAll, afterEach } from 'vitest';
beforeAll(() => {
// Runs once before all tests
});
afterEach(() => {
// Runs after each test
});
afterAll(() => {
// Runs once after all tests
});
// src/test/global-setup.ts
export default async function setup() {
// Global setup (runs before all test files)
console.log('Starting test suite');
}
export async function teardown() {
// Global teardown (runs after all test files)
console.log('Test suite complete');
}
Coverage Configuration
Comprehensive Coverage Setup
export default defineConfig({
test: {
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html', 'lcov'],
reportsDirectory: './coverage',
exclude: [
'node_modules/',
'dist/',
'**/*.test.ts',
'**/*.test.tsx',
'**/__tests__/**',
'**/__mocks__/**',
'**/*.d.ts',
'**/types/**',
'**/index.ts', // Re-export files
],
thresholds: {
lines: 80,
functions: 80,
branches: 80,
statements: 80,
},
// Fail if coverage drops
thresholds: {
'100': true, // Enforce 100% on new code
},
},
},
});
TypeScript Integration
Type Checking in Tests
export default defineConfig({
test: {
typecheck: {
enabled: true,
tsconfig: './tsconfig.json',
include: ['**/*.test.ts', '**/*.test.tsx'],
},
},
});
tsconfig.json for Tests
{
"compilerOptions": {
"types": ["vitest/globals"]
},
"include": ["src/**/*", "src/**/__tests__/**/*"]
}
Path Aliases
import { defineConfig } from 'vitest/config';
import path from 'node:path';
export default defineConfig({
test: {
alias: {
'@': path.resolve(__dirname, './src'),
'@test': path.resolve(__dirname, './src/test'),
},
},
});
Parallel Execution
export default defineConfig({
test: {
// Run tests in parallel (default)
pool: 'threads',
poolOptions: {
threads: {
singleThread: false,
maxThreads: 4,
minThreads: 1,
},
},
// Or run tests sequentially
// sequence: {
// concurrent: false,
// },
},
});
Test Filtering
By Pattern
# Run tests matching pattern
vitest run --grep="user"
# Run specific file
vitest run src/utils/__tests__/format.test.ts
By Annotation
import { describe, it, expect } from 'vitest';
describe('feature', () => {
it.skip('skipped test', () => {
// This test is skipped
});
it.only('only this test runs', () => {
// Only this test runs when using .only
});
it.todo('not yet implemented');
});
Snapshot Testing
import { describe, it, expect } from 'vitest';
describe('snapshots', () => {
it('should match snapshot', () => {
const result = { id: 1, name: 'test' };
expect(result).toMatchSnapshot();
});
it('should match inline snapshot', () => {
const result = { id: 1, name: 'test' };
expect(result).toMatchInlineSnapshot(`
{
"id": 1,
"name": "test",
}
`);
});
});
Snapshot Configuration
export default defineConfig({
test: {
snapshotFormat: {
printBasicPrototype: false,
},
},
});
Mocking Configuration
Auto-Mocking
export default defineConfig({
test: {
mockReset: true, // Reset mocks before each test
clearMocks: true, // Clear mock calls before each test
restoreMocks: true, // Restore original implementations
},
});
Module Mocking Directory
export default defineConfig({
test: {
deps: {
interopDefault: true,
},
},
});
Reporter Configuration
export default defineConfig({
test: {
reporters: ['default', 'json', 'html'],
outputFile: {
json: './test-results/results.json',
html: './test-results/results.html',
},
},
});
Watch Mode Configuration
export default defineConfig({
test: {
watch: true,
watchExclude: ['**/node_modules/**', '**/dist/**'],
forceRerunTriggers: ['**/vitest.config.ts', '**/vite.config.ts'],
},
});
CI Configuration
export default defineConfig({
test: {
// CI-specific settings
...(process.env.CI
? {
minWorkers: 1,
maxWorkers: 2,
coverage: {
reporter: ['text', 'json', 'lcov'],
},
}
: {}),
},
});
Vitest UI
npm install -D @vitest/ui
vitest --ui
Opens interactive UI at http://localhost:51204/__vitest__/
Migration from Jest
Configuration Mapping
| Jest | Vitest |
|---|---|
testEnvironment | environment |
setupFilesAfterEnv | setupFiles |
testMatch | include |
testPathIgnorePatterns | exclude |
moduleNameMapper | alias |
collectCoverageFrom | coverage.include |
Import Changes
// Jest
import { jest } from '@jest/globals';
// Vitest
import { vi } from 'vitest';
// Jest mock
jest.fn();
jest.mock('./module');
// Vitest mock
vi.fn();
vi.mock('./module');
Best Practices Summary
- Use TypeScript for configuration
- Set coverage thresholds to 80%+
- Enable type checking in tests
- Use appropriate test environment
- Configure parallel execution
- Use setup files for common setup
- Configure reporters for CI
Code Review Checklist
- vitest.config.ts uses TypeScript
- Coverage thresholds set (80%+)
- Type checking enabled
- Correct environment selected
- Setup files configured if needed
- Snapshot format configured
- CI-specific settings applied
スコア
総合スコア
60/100
リポジトリの品質指標に基づく評価
✓SKILL.md
SKILL.mdファイルが含まれている
+20
✓LICENSE
ライセンスが設定されている
+10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
レビュー
💬
レビュー機能は近日公開予定です