vitest
1
总安装量
1
周安装量
#52033
全站排名
安装命令
npx skills add https://github.com/phrazzld/claude-config --skill vitest
Agent 安装分布
mcpjam
1
claude-code
1
replit
1
junie
1
zencoder
1
Skill 文档
Vitest Best Practices
For test philosophy (behavior vs implementation, TDD workflow, when to mock), see /testing-philosophy.
Critical Rules
- Node 22+: Use
pool: 'forks'– threads have known issues - CI optimization: Single worker, disable watch, enable
isolate: falseif safe - Coverage: Always define
coverage.include– defaults exclude too much - Mocking: Prefer
vi.spyOnovervi.mock– avoids hoisting footguns - RTL cleanup: Requires
globals: truein config
Quick Reference
Pool Selection (Node 22+)
| Pool | Use When | Avoid When |
|---|---|---|
forks |
Node 22+, default choice | – |
threads |
Node <22, CPU-bound tests | Node 22+ (native fetch issues) |
vmThreads |
Need isolation + speed | Memory-constrained CI |
CI Configuration
export default defineConfig({
test: {
pool: 'forks',
poolOptions: {
forks: {
singleFork: true, // CI: predictable, less overhead
},
},
isolate: false, // Faster if tests don't leak state
reporters: ['verbose'],
coverage: {
reportOnFailure: true,
},
},
})
Coverage Quick Reference
coverage: {
provider: 'v8', // Accurate in Vitest 3.2+
include: ['src/**'], // ALWAYS define - defaults miss files
reporter: ['text', 'lcov'],
reportOnFailure: true, // Get coverage even on test failure
}
Mocking Quick Reference
// PREFER: vi.spyOn - explicit, no hoisting issues
const spy = vi.spyOn(service, 'method').mockReturnValue('mocked')
// AVOID unless necessary: vi.mock - hoisted, can't use imports
vi.mock('./module', () => ({ fn: vi.fn() }))
Reference Files
- Pool Configuration – threads vs forks vs vmThreads
- Performance Patterns – CI optimization, sharding
- Coverage Strategy – v8 vs Istanbul, thresholds
- Mocking Pitfalls – vi.mock hoisting, cleanup