vitest-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVitest Testing
Vitest 测试
Overview
概述
Vitest is a Vite-native unit testing framework that shares the same configuration and plugin ecosystem. Built for speed with native ESM support, hot module replacement for tests, and parallel execution.
When to use: Unit tests, component tests, integration tests, hook tests, in-source tests. Testing React components with React Testing Library. Testing TanStack Query/Router/Form patterns.
When NOT to use: End-to-end testing (use Playwright), visual regression testing (use Percy/Chromatic), load testing (use k6).
Vitest 是一款原生基于 Vite 的单元测试框架,共享相同的配置和插件生态系统。专为速度优化,支持原生 ESM、测试热模块替换和并行执行。
适用场景: 单元测试、组件测试、集成测试、钩子测试、源内测试。使用 React Testing Library 测试 React 组件。测试 TanStack Query/Router/Form 模式。
不适用场景: 端到端测试(请使用 Playwright)、视觉回归测试(请使用 Percy/Chromatic)、负载测试(请使用 k6)。
Quick Reference
快速参考
| Pattern | API | Key Points |
|---|---|---|
| Test structure | | Organize related tests |
| Single test | | Both are aliases |
| Parameterized tests | | Run same test with different inputs |
| Concurrent tests | | Run tests in parallel |
| Skip test | | Conditionally skip tests |
| Only test | | Run specific test for debugging |
| Assertions | | Compare primitive values |
| Deep equality | | Compare objects/arrays |
| Async assertions | | Test promise resolution |
| Before each test | | Setup before each test |
| After each test | | Cleanup after each test |
| Mock function | | Create spy function |
| Mock module | | Replace entire module |
| Spy on method | | Track calls to existing method |
| Clear mocks | | Clear call history |
| Fake timers | | Control setTimeout/setInterval |
| Render component | | Mount React component for testing |
| Query by role | | Find elements by accessibility role |
| User interaction | | Simulate user events |
| Wait for change | | Wait for async changes |
| Find async element | | Query + wait combined |
| Render hook | | Test custom hooks |
| Update hook props | | Re-render hook with new props |
| Snapshot | | Compare against saved snapshot |
| Inline snapshot | | Snapshot stored in test file |
| 模式 | API | 关键要点 |
|---|---|---|
| 测试结构 | | 组织相关测试用例 |
| 单个测试用例 | | 两者是别名 |
| 参数化测试 | | 使用不同输入运行相同测试用例 |
| 并发测试 | | 并行运行测试用例 |
| 跳过测试 | | 有条件地跳过测试用例 |
| 仅运行指定测试 | | 运行特定测试用例以进行调试 |
| 断言 | | 比较原始值 |
| 深度相等 | | 比较对象/数组 |
| 异步断言 | | 测试 Promise 解析结果 |
| 每个测试前执行 | | 在每个测试前进行设置 |
| 每个测试后执行 | | 在每个测试后进行清理 |
| 模拟函数 | | 创建间谍函数 |
| 模拟模块 | | 替换整个模块 |
| 监听方法 | | 跟踪现有方法的调用情况 |
| 清除模拟 | | 清除调用历史 |
| 假定时器 | | 控制 setTimeout/setInterval |
| 渲染组件 | | 挂载 React 组件以进行测试 |
| 按角色查询 | | 按无障碍角色查找元素 |
| 用户交互 | | 模拟用户事件 |
| 等待变更 | | 等待异步变更 |
| 查找异步元素 | | 查询与等待的结合 |
| 渲染钩子 | | 测试自定义钩子 |
| 更新钩子属性 | | 使用新属性重新渲染钩子 |
| 快照 | | 与保存的快照进行比较 |
| 内联快照 | | 快照存储在测试文件中 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Using | Use |
| Testing implementation details | Test behavior and public API |
Using | Prefer |
Missing | Always call |
| Shared state between tests | Use |
| Not cleaning up mocks | Use |
| Mocking too much | Only mock external dependencies and APIs |
| Not disabling retries in tests | Set |
| Immediate assertions on async | Use |
| Creating QueryClient in render | Create once in wrapper or use |
| Testing library code | Trust the library, test your usage |
| Not awaiting user events | All |
Using | |
| Inline select without memoization | Extract to stable function for TanStack Query |
| 错误 | 正确做法 |
|---|---|
对异步内容使用 | 对异步内容使用 |
| 测试实现细节 | 测试行为和公共 API |
优先使用 | 优先使用 |
遗漏 | 始终先调用 |
| 测试间共享状态 | 使用 |
| 未清理模拟 | 在 |
| 过度模拟 | 仅模拟外部依赖和 APIs |
| 未在测试中禁用重试 | 为 TanStack Query 测试设置 |
| 对异步内容立即断言 | 使用 |
| 在渲染中创建 QueryClient | 在包装器中创建一次或使用 |
| 测试库代码 | 信任库,测试你的使用方式 |
| 未等待用户事件 | 所有 |
手动使用 | |
| 内联选择器未进行记忆化 | 提取为稳定函数以用于 TanStack Query |
Delegation
任务委托
- Test discovery: For finding untested code paths, use agent
Explore - Coverage analysis: For comprehensive coverage review, use agent
Task - E2E testing: If skill is available, delegate E2E testing to it
playwright - Code review: After writing tests, delegate to agent
code-reviewer
- 测试发现:若要查找未测试的代码路径,请使用 agent
Explore - 覆盖率分析:若要进行全面的覆盖率审查,请使用 agent
Task - 端到端测试:如果有 技能,请将端到端测试委托给它
playwright - 代码审查:编写完测试后,委托给 agent
code-reviewer
References
参考资料
- Test fundamentals: structure, assertions, lifecycle hooks
- Mocking: vi.fn, vi.mock, vi.spyOn, module mocking
- Component testing: React Testing Library, queries, user-event
- Hook testing: renderHook, async hooks, TanStack patterns
- Configuration: vitest.config.ts, workspace, coverage, reporters
- Advanced patterns: snapshots, concurrent tests, in-source, type testing
- 测试基础:结构、断言、生命周期钩子
- 模拟:vi.fn、vi.mock、vi.spyOn、模块模拟
- 组件测试:React Testing Library、查询、user-event
- 钩子测试:renderHook、异步钩子、TanStack 模式
- 配置:vitest.config.ts、工作区、覆盖率、报告器
- 高级模式:快照、并发测试、源内测试、类型测试