vitest
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVitest Best Practices
Vitest 最佳实践
Comprehensive performance optimization and best practices guide for Vitest testing framework. Contains 44 rules across 8 categories, prioritized by impact to guide test writing, refactoring, and code review.
这是一份针对Vitest测试框架的全面性能优化与最佳实践指南。包含8个类别共44条规则,按影响优先级排序,可指导测试编写、重构及代码评审。
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
在以下场景中可参考本指南:
- 编写新的Vitest测试
- 调试不稳定或缓慢的测试
- 配置测试环境
- 评审PR中的测试代码
- 从Jest迁移至Vitest
- 优化CI/CD中的测试性能
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 | Mocking模式 | 高 | |
| 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 - - 每次测试后恢复Mock
setup-restore-mocks - - 避免测试间共享可变状态
setup-avoid-shared-state - - 使用beforeAll执行耗时的一次性设置
setup-beforeall-expensive - - 测试模块状态时重置模块
setup-reset-modules - - 使用测试工厂生成复杂测试数据
setup-test-factories
3. Mocking Patterns (HIGH)
3. Mocking模式(高)
- - 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
mock-implementation-not-value - - 使用MSW进行网络请求Mocking
mock-msw-network - - 避免过度Mock
mock-avoid-overmocking - - 保持Mock的类型安全性
mock-type-safety - - 测试间清理Mock状态
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中使用快速失败机制
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 - - Mock测试环境中不存在的浏览器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:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
- async-await-assertions - Example rule file
- mock-vi-mock-hoisting - Example rule file
阅读单个参考文件获取详细说明及代码示例:
- Section definitions - 类别结构与影响级别说明
- Rule template - 添加新规则的模板
- async-await-assertions - 规则示例文件
- mock-vi-mock-hoisting - 规则示例文件
Related Skills
相关技能
- For TDD methodology, see skill
test-tdd - For API mocking with MSW, see skill
test-msw - For TypeScript testing patterns, see skill
typescript
- 如需了解TDD方法论,请查看技能
test-tdd - 如需基于MSW的API Mocking,请查看技能
test-msw - 如需TypeScript测试模式,请查看技能
typescript
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md包含所有规则详细内容的完整指南:
AGENTS.md