Vitest 基础
设置 Vitest, 编写你的首先测试, 运行 watcher, and 读取 results.
你将学到
- 如何 安装 and 配置 Vitest in Vite 项目
- 编写描述, 它, and expect 块
- 运行测试 in watch 模式 and in CI
- 读取 and interpreting terminal report
概念讲解
Why Vitest
Vitest 是一种test framework built on Vite。 Because it reuses your project's Vite pipeline, your tests transform TypeScript, JSX, and CSS exactly the way your app does — no separate babel or ts-jest config to drift out of sync. It is also extremely fast: tests run in parallel worker threads and transform via esbuild, so even large suites finish in seconds.
Installation and Config
Add Vitest with pnpm 添加 -D vitest. If your project already has a vite.配置.ts, Vitest reads it automatically; you only add a 测试 section for test-specific options like the environment (jsdom for DOM tests) or globals. Add scripts to package.json: "测试": "vitest" for watch mode and "测试:运行": "vitest 运行" for a single CI pass.
Anatomy of a Test
A test file imports 描述, 它, and expect from vitest. 描述 groups related tests, 它 (or 测试) defines one case, and expect chains matchers like toBe, toEqual, and toThrow. Vitest's matcher API is Jest-compatible, so existing Jest knowledge transfers directly.
Running and Reading Results
In watch mode, Vitest reruns only the tests affected by your changed files, giving sub-second feedback. The terminal report shows a checkmark or cross per test, the failing assertion's expected vs actual values, and the source location of the failure. In CI, vitest 运行 exits with a non-zero code on failure, which is what your pipeline needs to gate merges.
In Practice
A typical first session: install Vitest, add a math.测试.ts next to your math.ts, run npx vitest, and watch the terminal. Edit the source file and the test reruns instantly — this tight loop is addictive and is the core of why developers who try modern test runners never go back. Configure coverage with the v8 provider for fast, accurate reports, and set thresholds so coverage cannot silently drop. For DOM and component tests, switch the environment to jsdom or happy-DOM either in the config or with a per-file // @vitest-环境 jsdom comment. Use vi.mock for module mocks and vi.fn for function doubles — you will meet these in the test-doubles lessons. Keep the config minimal; Vitest's defaults are good, and fighting the framework's opinions usually signals a design problem in the code under test.
示例代码
// vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "node", // use "jsdom" for DOM/component tests
globals: false, // prefer explicit imports
},
});
// src/math.test.ts
import { describe, it, expect } from "vitest";
export function factorial(n: number): number {
return n <= 1 ? 1 : n * factorial(n - 1);
}
describe("factorial", () => {
it("computes 5! as 120", () => {
expect(factorial(5)).toBe(120);
});
});
配置是 minimal 因为 Vitest inherits Vite 设置. 运行 `npx vitest` 到开始 watching; 编辑 file and relevant 测试 reruns instantly.
动手试一试
- Code Diff
Diff expected 对比实际当 Vitest assertion 失败.
- JSON Formatter
格式 JSON 输出从 `vitest 运行 --reporter=JSON`.
常见错误
错误写法
Forgetting 到设置环境 for DOM 测试
正确做法
组件测试需要 `环境: 'jsdom'` (or 'happy-DOM') in 配置 or `// @vitest-环境 jsdom` comment, or document 是 undefined.
错误写法
Leaving watch 模式运行 in CI
正确做法
Use `vitest 运行` (单个通过) in CI scripts and `vitest` (watch) 仅 locally. Watch 模式从不 exits and hangs 流水线.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。