名称: vitest 描述: 提供Vitest测试框架的全面指导,包括快速测试执行、Vite集成、组件测试、模拟和配置。当用户询问Vitest、需要编写快速单元测试、测试Vue/React组件或在Vite项目中配置Vitest时使用。 许可证: 完整条款见LICENSE.txt
何时使用此技能
在用户想要以下情况时使用此技能:
- 在Vite项目中设置Vitest
- 编写单元测试和组件测试
- 为不同环境配置Vitest
- 使用Vitest API和工具
- 测试Vue、React或Svelte组件
- 使用浏览器模式进行测试
- 设置视觉回归测试
- 模拟函数和模块
- 使用快照进行测试
- 配置测试覆盖率
- 使用Vitest UI
- 优化测试性能
- 调试测试
- 理解Vitest最佳实践
- 解决Vitest问题
如何使用此技能
此技能组织方式与Vitest官方文档结构匹配(https://vitest.dev/guide/, https://vitest.dev/api/, https://vitest.dev/config/)。使用Vitest时:
-
从用户请求中识别主题:
- 快速开始 →
examples/getting-started.md - 功能特性 →
examples/features.md - 组件测试 →
examples/component-testing.md - 浏览器模式 →
examples/browser-mode.md - API文档 →
api/ - 配置 →
examples/config/
- 快速开始 →
-
从
examples/目录加载适当的示例文件:快速开始:
examples/getting-started.md- 安装和第一个测试
功能特性:
examples/features.md- 关键特性和功能examples/ui.md- Vitest UIexamples/component-testing.md- 组件测试examples/browser-mode.md- 浏览器模式测试examples/visual-regression-testing.md- 视觉回归测试examples/trace-view.md- 跟踪视图
测试:
examples/test-api.md- 测试API(test, it, describe等)examples/mocking.md- 模拟函数和模块examples/snapshots.md- 快照测试examples/coverage.md- 代码覆盖率
配置:
examples/config/basic-config.md- 基本配置examples/config/environment.md- 环境配置examples/config/browser-config.md- 浏览器模式配置
-
遵循该示例文件中的具体指示,包括语法、结构和最佳实践
重要注意事项:
- Vitest专为Vite项目设计
- 支持TypeScript、JSX、ESM开箱即用
- 快速监视模式,类似HMR体验
- 与Jest API兼容
- 示例包括JavaScript和TypeScript版本
- 每个示例文件包含关键概念、代码示例和要点
-
需要时参考
api/目录中的API文档:api/test-api.md- 测试API参考api/vi-utility.md- vi工具函数api/expect.md- Expect断言api/mocking.md- 模拟API
-
使用
templates/目录中的模板:templates/vitest-config.md- Vitest配置模板templates/test-examples.md- 测试示例模板
文档映射(与https://vitest.dev/guide/, https://vitest.dev/api/, https://vitest.dev/config/一对一对应)
指南:
examples/getting-started.md→ https://vitest.dev/guide/getting-started.htmlexamples/features.md→ https://vitest.dev/guide/features.htmlexamples/ui.md→ https://vitest.dev/guide/ui.htmlexamples/component-testing.md→ https://vitest.dev/guide/testing-components.htmlexamples/browser-mode.md→ https://vitest.dev/guide/browser.htmlexamples/visual-regression-testing.md→ https://vitest.dev/guide/visual-regression.htmlexamples/trace-view.md→ https://vitest.dev/guide/trace-view.htmlexamples/mocking.md→ https://vitest.dev/guide/mocking.htmlexamples/snapshots.md→ https://vitest.dev/guide/snapshot.htmlexamples/coverage.md→ https://vitest.dev/guide/coverage.html
配置:
examples/config/basic-config.md→ https://vitest.dev/config/examples/config/environment.md→ https://vitest.dev/config/#environmentexamples/config/browser-config.md→ https://vitest.dev/config/#browser
API参考:
api/test-api.md→ https://vitest.dev/api/api/vi-utility.md→ https://vitest.dev/api/vi.htmlapi/expect.md→ https://vitest.dev/api/expect.htmlapi/mocking.md→ https://vitest.dev/api/vi.html
示例和模板
此技能包括详细示例,组织方式与官方文档结构匹配。所有示例在examples/目录中(见上述映射)。
使用示例:
- 从用户请求中识别主题
- 从上述映射加载适当的示例文件
- 遵循该文件中的指示、语法和最佳实践
- 将代码示例适应到您的具体用例
使用模板:
- 参考
templates/目录中的模板进行常见脚手架 - 将模板适应到您的具体需求和编码风格
最佳实践
- 使用监视模式:利用Vitest的智能监视模式获取更快反馈
- 组织测试:使用describe块分组相关测试
- 使用TypeScript:利用原生TypeScript支持
- 有效模拟:使用vi.mock()进行模块模拟
- 测试组件:使用组件测试测试Vue/React组件
- 使用UI模式:使用
--ui标志获得更好的测试调试体验 - 优化性能:在开发过程中使用test.only()
资源
- 官方文档:https://vitest.dev/
- 快速开始:https://vitest.dev/guide/
- API参考:https://vitest.dev/api/
- 配置:https://vitest.dev/config/
- GitHub仓库:https://github.com/vitest-dev/vitest
关键词
Vitest, vitest, 测试框架, 单元测试, 组件测试, Vite, Jest兼容, 监视模式, HMR, TypeScript, ESM, 模拟, 快照, 覆盖率, 浏览器模式, 视觉回归测试