vitest-v4
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 - - 对依赖时间的代码使用 fake timers
async-fake-timers - - 使用 vi.waitFor 等待异步条件
async-waitfor-polling - - 在并发测试中使用测试上下文的 expect
async-concurrent-expect - - 等待 user events 完成,避免 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 hook 中清理状态
setup-beforeeach-cleanup - - 每个测试后恢复 mocks
setup-restore-mocks - - 避免测试之间共享可变状态
setup-avoid-shared-state - - 使用 beforeAll 执行高开销的一次性设置
setup-beforeall-expensive - - 测试模块状态时重置模块
setup-reset-modules - - 使用 test factories 创建复杂测试数据
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 - - 对动态 mocks 使用 mockImplementation
mock-implementation-not-value - - 使用 MSW Mock 网络请求
mock-msw-network - - 避免过度 Mocking
mock-avoid-overmocking - - 在 mocks 中保持类型安全
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
- - 选择适合性能需求的 pool
perf-pool-selection - - 在安全的情况下禁用测试隔离
perf-disable-isolation - - 尽可能使用 happy-dom 而非 jsdom
perf-happy-dom - - 使用 sharding 实现 CI 并行化
perf-sharding - - 在 CI 环境中使用 run 模式
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
- - 对小型值优先使用 inline snapshots
snap-inline-over-file - - 避免大型 snapshots
snap-avoid-large - - 确保快照序列化结果稳定
snap-stable-serialization - - 提交前审查 snapshot 更新
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 - - 使用 setup files 进行全局配置
env-setup-files - - 统一配置 globals
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
- - 使用具体的 matchers,而非通用 matchers
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 blocks 对相关内容进行逻辑分组
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
阅读单独的参考文件以获取详细说明和代码示例:
- 类别定义 - 类别结构和影响等级
- 规则模板 - 用于添加新规则的模板
- async-await-assertions - 示例规则文件
- mock-vi-mock-hoisting - 示例规则文件
Interactions
交互关系
- Does not cover app Playwright E2E — see playwright-v1 for ad-hoc browser automation; app E2E conventions live in consuming-repo rules
- HTTP app tests with and database isolation (PGLite,
inject()vs*.spec.ts) are consuming-repo concerns — follow the project's Fastify testing rule, not this skill alone*.test.ts
- 不涵盖应用级 Playwright E2E——有关临时浏览器自动化,请参阅 playwright-v1;应用 E2E 约定由使用方仓库的规则定义
- 使用 inject() 和数据库隔离的 HTTP 应用测试(PGLite、与
*.spec.ts)属于使用方仓库的关注事项——应遵循项目的 Fastify 测试规则,而不应仅依赖本 skill*.test.ts
Related Skills
相关 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 方法论,请参阅 skill
test-tdd - 关于使用 MSW 进行 API Mocking,请参阅 skill
test-msw - 关于 TypeScript 测试模式,请参阅 skill
typescript
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
references/compiled.md如需查看包含所有规则详细展开内容的完整指南,请参阅
references/compiled.md