vitest
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVitest Best Practices
Vitest 最佳实践
Vitest 4-compatible testing guide with 44 rules across 8 categories. Vitest 4 requires Node.js 20+ and Vite 6+ when used with Vite.
兼容 Vitest 4 的测试指南,包含8个分类下的44条规则。使用 Vite 时,Vitest 4 需要 Node.js 20+ 和 Vite 6+ 版本。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Vitest tests
- Debugging flaky or slow tests
- Setting up test configuration
- Reviewing test code in PRs
- Migrating from Jest to Vitest
- Optimizing CI/CD test performance
Jest-specific APIs and configuration outside documented migration differences are out of scope; use Jest's own documentation for tests that still run under Jest.
在以下场景中可参考本指南:
- 编写新的 Vitest 测试用例
- 调试不稳定或运行缓慢的测试
- 配置测试环境
- 审查 PR 中的测试代码
- 从 Jest 迁移至 Vitest
- 优化 CI/CD 中的测试性能
超出文档记录的迁移差异之外的 Jest 专属 API 和配置不在本指南范围内;仍在 Jest 下运行的测试请参考 Jest 官方文档。
Rule Categories by Priority
按优先级划分的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Async Patterns | CRITICAL | |
| 2 | Test Setup & Isolation | CRITICAL | |
| 3 | Mocking Patterns | HIGH | |
| 4 | Performance | HIGH | |
| 5 | Snapshot Testing | MEDIUM | |
| 6 | Environment | MEDIUM | |
| 7 | Assertions | LOW-MEDIUM | |
| 8 | Test Organization | LOW | |
| 优先级 | 分类 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 异步模式 | 关键 | |
| 2 | 测试设置与隔离 | 关键 | |
| 3 | 模拟模式 | 高 | |
| 4 | 性能 | 高 | |
| 5 | 快照测试 | 中 | |
| 6 | 环境 | 中 | |
| 7 | 断言 | 低-中 | |
| 8 | 测试组织 | 低 | |
Quick Reference
快速参考
1. Async Patterns (CRITICAL)
1. 异步模式(关键)
- - Await async assertions to prevent false positives
async-await-assertions - - Return promises from test functions
async-return-promises - - Use fake timers for time-dependent code
async-fake-timers - - Use vi.waitFor for async conditions
async-waitfor-polling - - Use test context expect in concurrent tests
async-concurrent-expect - - Await user events to avoid act warnings
async-act-wrapper - - Test async error handling properly
async-error-handling
- - 等待异步断言完成,避免误报
async-await-assertions - - 从测试函数返回 Promise
async-return-promises - - 为依赖时间的代码使用假计时器
async-fake-timers - - 使用 vi.waitFor 处理异步条件
async-waitfor-polling - - 在并发测试中使用测试上下文的 expect
async-concurrent-expect - - 等待用户事件完成,避免 act 警告
async-act-wrapper - - 正确测试异步错误处理逻辑
async-error-handling
2. Test Setup & Isolation (CRITICAL)
2. 测试设置与隔离(关键)
- - Clean up state in afterEach hooks
setup-beforeeach-cleanup - - Restore mocks after each test
setup-restore-mocks - - Avoid shared mutable state between tests
setup-avoid-shared-state - - Use beforeAll for expensive one-time setup
setup-beforeall-expensive - - Reset modules when testing module state
setup-reset-modules - - Use test factories for complex test data
setup-test-factories
- - 在 afterEach 钩子中清理状态
setup-beforeeach-cleanup - - 每次测试后恢复模拟
setup-restore-mocks - - 避免测试间共享可变状态
setup-avoid-shared-state - - 使用 beforeAll 处理耗时的一次性设置
setup-beforeall-expensive - - 测试模块状态时重置模块
setup-reset-modules - - 使用测试工厂生成复杂测试数据
setup-test-factories
3. Mocking Patterns (HIGH)
3. 模拟模式(高)
- - Understand vi.mock hoisting behavior
mock-vi-mock-hoisting - - Choose vi.spyOn vs vi.mock appropriately
mock-spyon-vs-mock - - Use mockImplementation for dynamic mocks
mock-implementation-not-value - - Use MSW for network request mocking
mock-msw-network - - Avoid over-mocking
mock-avoid-overmocking - - Maintain type safety in mocks
mock-type-safety - - Clear mock state between tests
mock-clear-between-tests
- - 理解 vi.mock 的提升行为
mock-vi-mock-hoisting - - 合理选择 vi.spyOn 和 vi.mock
mock-spyon-vs-mock - - 使用 mockImplementation 创建动态模拟
mock-implementation-not-value - - 使用 MSW 进行网络请求模拟
mock-msw-network - - 避免过度模拟
mock-avoid-overmocking - - 保持模拟的类型安全性
mock-type-safety - - 测试间清除模拟状态
mock-clear-between-tests
4. Performance (HIGH)
4. 性能(高)
- - Choose the right pool for performance
perf-pool-selection - - Disable test isolation when safe
perf-disable-isolation - - Use happy-dom over jsdom when possible
perf-happy-dom - - Use sharding for CI parallelization
perf-sharding - - Use run mode in CI environments
perf-run-mode-ci - - Use bail for fast failure in CI
perf-bail-fast-fail
- - 选择合适的工作池以提升性能
perf-pool-selection - - 在安全情况下禁用测试隔离
perf-disable-isolation - - 尽可能使用 happy-dom 替代 jsdom
perf-happy-dom - - 在 CI 中使用分片实现并行化
perf-sharding - - 在 CI 环境中使用运行模式
perf-run-mode-ci - - 在 CI 中使用 bail 快速失败机制
perf-bail-fast-fail
5. Snapshot Testing (MEDIUM)
5. 快照测试(中)
- - Prefer inline snapshots for small values
snap-inline-over-file - - Avoid large snapshots
snap-avoid-large - - Ensure stable snapshot serialization
snap-stable-serialization - - Review snapshot updates before committing
snap-review-updates - - Name snapshot tests descriptively
snap-describe-intent
- - 小值优先使用内联快照
snap-inline-over-file - - 避免使用大型快照
snap-avoid-large - - 确保快照序列化的稳定性
snap-stable-serialization - - 提交前审查快照更新
snap-review-updates - - 为快照测试命名时清晰描述意图
snap-describe-intent
6. Environment (MEDIUM)
6. 环境(中)
- - Override environment per file when needed
env-per-file-override - - Use setup files for global configuration
env-setup-files - - Configure globals consistently
env-globals-config - - Mock browser APIs not available in test environment
env-browser-api-mocking
- - 必要时按文件覆盖环境配置
env-per-file-override - - 使用设置文件进行全局配置
env-setup-files - - 统一配置全局变量
env-globals-config - - 模拟测试环境中不可用的浏览器 API
env-browser-api-mocking
7. Assertions (LOW-MEDIUM)
7. 断言(低-中)
- - Use specific matchers over generic ones
assert-specific-matchers - - Test edge cases and boundaries
assert-edge-cases - - Test one concept per test
assert-one-assertion-concept - - Use expect.assertions for async tests
assert-expect-assertions - - Choose toBe vs toEqual correctly
assert-toequal-vs-tobe
- - 使用特定断言替代通用断言
assert-specific-matchers - - 测试边界情况
assert-edge-cases - - 每个测试仅验证一个概念
assert-one-assertion-concept - - 在异步测试中使用 expect.assertions
assert-expect-assertions - - 正确选择 toBe 和 toEqual
assert-toequal-vs-tobe
8. Test Organization (LOW)
8. 测试组织(低)
- - Colocate test files with source files
org-file-colocation - - Use describe blocks for logical grouping
org-describe-nesting - - Write descriptive test names
org-test-naming - - Use skip and only appropriately
org-test-skip-only
- - 将测试文件与源文件放在同一目录
org-file-colocation - - 使用 describe 块进行逻辑分组
org-describe-nesting - - 编写描述性的测试名称
org-test-naming - - 合理使用 skip 和 only
org-test-skip-only
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- async-await-assertions - Example rule file
- mock-vi-mock-hoisting - Example rule file
阅读单个参考文件获取详细说明和代码示例:
- async-await-assertions - 规则示例文件
- mock-vi-mock-hoisting - 规则示例文件
Related Skills
相关技能
- For TDD methodology, see skill
tdd - For API mocking with MSW, see skill
msw - For TypeScript testing patterns, see skill
typescript
- TDD 方法论请参考 技能文档
tdd - MSW API 模拟请参考 技能文档
msw - TypeScript 测试模式请参考 技能文档
typescript
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有规则扩展说明的完整指南,请查看:
AGENTS.md