vitest
它能做什么
这项技能说明如何使用 Vitest 编写和运行自动化测试。Vitest 是一个与 Vite 项目配合使用的测试工具。适合想要快速测试、并且风格类似 Jest 的开发者。
适用场景
- ✓当你需要为函数或组件编写单元测试时。
- ✓当你想添加模拟、间谍或快照测试时。
- ✓当你想设置测试覆盖率或选择运行哪些测试的时候。
技能链接
https://github.com/antfu/skills/blob/main/skills/vitest/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请为我的价格计算函数编写 Vitest 单元测试,包括边界情况。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体使用 Vitest 的 test、describe 和 expect 函数,并遵循项目的配置。在监视模式下,它只重新运行受改动影响的测试。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Vitest is a next-generation testing framework powered by Vite. It provides a Jest-compatible API with native ESM, TypeScript, and JSX support out of the box. Vitest shares the same config, transformers, resolvers, and plugins with your Vite app.
Key Features:
- Vite-native: Uses Vite's transformation pipeline for fast HMR-like test updates
- Jest-compatible: Drop-in replacement for most Jest test suites
- Smart watch mode: Only reruns affected tests based on module graph
- Native ESM, TypeScript, JSX support without configuration
- Multi-threaded workers for parallel test execution
- Built-in coverage via V8 or Istanbul
- Snapshot testing, mocking, and spy utilities
The skill is based on Vitest 5.1.0-beta.1, generated at 2026-10-23.
Core
| Topic | Description | Reference |
|---|---|---|
| Configuration | Vitest and Vite config integration, defineConfig usage | core-config |
| CLI | Command line interface, commands and options | core-cli |
| Test API | test/it function, modifiers like skip, only, concurrent | core-test-api |
| Describe API | describe/suite for grouping tests and nested suites | core-describe |
| Expect API | Assertions with toBe, toEqual, matchers and asymmetric matchers | core-expect |
| Hooks | beforeEach, afterEach, beforeAll, afterAll, aroundEach | core-hooks |
Features
| Topic | Description | Reference |
|---|---|---|
| Mocking | Mock functions, modules, timers, dates with vi utilities | features-mocking |
| Snapshots | Snapshot testing with toMatchSnapshot and inline snapshots | features-snapshots |
| Coverage | Code coverage with V8 or Istanbul providers | features-coverage |
| Test Context | Test fixtures, context.expect, test.extend for custom fixtures | features-context |
| Concurrency | Concurrent tests, parallel execution, sharding | features-concurrency |
| Filtering | Filter tests by name, file patterns, tags | features-filtering |
| Test Tags | Label tests with tags to filter runs and apply shared options | features-test-tags |
| Reporters | Built-in reporters, default selection, CI/output config | features-reporters |
| Benchmarking | Write benchmarks with the bench fixture (Tinybench) | features-benchmarking |
Advanced
| Topic | Description | Reference |
|---|---|---|
| Vi Utilities | vi helper: mock, spyOn, fake timers, hoisted, waitFor | advanced-vi |
| Environments | Test environments: node, jsdom, happy-dom, custom | advanced-environments |
| Type Testing | Type-level testing with expectTypeOf and assertType | advanced-type-testing |
| Projects | Multi-project workspaces, different configs per project | advanced-projects |
常见问题
Vitest 和 Jest 一样吗?
很相似。它的 API 与大多数 Jest 测试套件兼容,因此许多测试可以轻松迁移。
它能衡量测试覆盖率吗?
能。覆盖率可以用 V8 或 Istanbul 收集。
使用 TypeScript 需要额外设置吗?
不需要。TypeScript、ES 模块和 JSX 无需额外配置即可工作。
详情
作者的原始描述
Vitest fast unit testing framework powered by Vite with Jest-compatible API. Use when writing tests, mocking, configuring coverage, or working with test filtering and fixtures.
repo: antfu/skills
热度
- GitHub 星标
- 6.0k
- 排名
- #14447
- 许可证
- MIT
- 更新时间
- 2026/10/30
评论
- …