Loading...
Loading...
Compare original and translation side by side
| Use this skill when... | Use another skill instead when... |
|---|---|
| Setting up or configuring Vitest | Writing E2E browser tests (use playwright-testing) |
| Writing unit/integration tests in TS/JS | Testing Python code (use python-testing) |
| Migrating from Jest to Vitest | Analyzing test quality (use test-quality-analysis) |
| Configuring coverage thresholds | Generating property-based tests (use property-based-testing) |
| Using mocks, spies, or fake timers | Validating test effectiveness (use mutation-testing) |
| 适用场景 | 应使用其他技能的场景 |
|---|---|
| 搭建或配置Vitest | 编写端到端浏览器测试(使用playwright-testing) |
| 使用TS/JS编写单元/集成测试 | 测试Python代码(使用python-testing) |
| 从Jest迁移到Vitest | 分析测试质量(使用test-quality-analysis) |
| 配置覆盖率阈值 | 生成基于属性的测试(使用property-based-testing) |
| 使用模拟、间谍或假计时器 | 验证测试有效性(使用mutation-testing) |
bun add --dev vitest
bun add --dev @vitest/coverage-v8 # Coverage (recommended)
bun add --dev happy-dom # DOM testing (optional)
bunx vitest --version # Verifybun add --dev vitest
bun add --dev @vitest/coverage-v8 # 代码覆盖率(推荐)
bun add --dev happy-dom # DOM测试(可选)
bunx vitest --version # 验证安装import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'node',
},
});import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'node',
},
});bunx vitest # Watch mode (default)
bunx vitest run # Run once (CI mode)
bunx vitest --coverage # With coverage
bunx vitest src/utils.test.ts # Specific file
bunx vitest -t "should add numbers" # Filter by name
bunx vitest related src/utils.ts # Related tests
bunx vitest -u # Update snapshots
bunx vitest bench # Benchmarks
bunx vitest --ui # UI modebunx vitest # 监听模式(默认)
bunx vitest run # 单次运行(CI模式)
bunx vitest --coverage # 生成覆盖率报告
bunx vitest src/utils.test.ts # 运行指定文件的测试
bunx vitest -t "should add numbers" # 按测试名称过滤
bunx vitest related src/utils.ts # 运行与指定文件相关的测试
bunx vitest -u # 更新快照
bunx vitest bench # 基准测试
bunx vitest --ui # UI模式import { describe, it, expect } from 'vitest';
import { add, multiply } from './math';
describe('math utils', () => {
it('should add two numbers', () => {
expect(add(2, 3)).toBe(5);
});
it('should multiply two numbers', () => {
expect(multiply(2, 3)).toBe(6);
});
});import { describe, it, expect } from 'vitest';
import { add, multiply } from './math';
describe('数学工具函数', () => {
it('应该正确计算两个数的和', () => {
expect(add(2, 3)).toBe(5);
});
it('应该正确计算两个数的乘积', () => {
expect(multiply(2, 3)).toBe(6);
});
});| Assertion | Description |
|---|---|
| Strict equality |
| Deep equality |
| Deep strict equality |
| Truthiness |
| Null checks |
| Numeric comparison |
| Float comparison |
| String matching |
| Array/string length |
| Object property |
| Partial object match |
| Error throwing |
| 断言方法 | 描述 |
|---|---|
| 严格相等判断 |
| 深度相等判断 |
| 严格深度相等判断 |
| 真假值判断 |
| Null/Undefined判断 |
| 数值比较 |
| 浮点数近似比较 |
| 字符串匹配 |
| 数组/字符串长度判断 |
| 对象属性存在判断 |
| 部分对象匹配 |
| 异常抛出判断 |
test('async test', async () => {
const data = await fetchData();
expect(data).toBe('expected');
});
test('promise resolves', async () => {
await expect(fetchData()).resolves.toBe('expected');
});
test('promise rejects', async () => {
await expect(fetchBadData()).rejects.toThrow('error');
});test('异步测试', async () => {
const data = await fetchData();
expect(data).toBe('expected');
});
test('Promise成功解析', async () => {
await expect(fetchData()).resolves.toBe('expected');
});
test('Promise拒绝', async () => {
await expect(fetchBadData()).rejects.toThrow('error');
});import { vi, test, expect } from 'vitest';
// Mock function
const mockFn = vi.fn();
mockFn.mockReturnValue(42);
// Mock module
vi.mock('./api', () => ({
fetchUser: vi.fn(() => Promise.resolve({ id: 1, name: 'John' })),
}));
// Mock timers
vi.useFakeTimers();
vi.advanceTimersByTime(1000);
vi.restoreAllMocks();
// Spy on method
const spy = vi.spyOn(object, 'method');import { vi, test, expect } from 'vitest';
// 模拟函数
const mockFn = vi.fn();
mockFn.mockReturnValue(42);
// 模拟模块
vi.mock('./api', () => ({
fetchUser: vi.fn(() => Promise.resolve({ id: 1, name: 'John' })),
}));
// 模拟计时器
vi.useFakeTimers();
vi.advanceTimersByTime(1000);
vi.restoreAllMocks();
// 监听方法
const spy = vi.spyOn(object, 'method');test('snapshot test', () => {
expect(data).toMatchSnapshot();
});
test('inline snapshot', () => {
expect(result).toMatchInlineSnapshot('5');
});
// Update snapshots: bunx vitest -utest('快照测试', () => {
expect(data).toMatchSnapshot();
});
test('内联快照', () => {
expect(result).toMatchInlineSnapshot('5');
});
// 更新快照:bunx vitest -ubun add --dev @vitest/coverage-v8
bunx vitest --coverageproviderreporterincludeexcludethresholdsbun add --dev @vitest/coverage-v8
bunx vitest --coverageproviderreporterincludeexcludethresholds| Context | Command |
|---|---|
| Quick test | |
| CI test | |
| Coverage check | |
| Single file | |
| Failed only | |
| 场景 | 命令 |
|---|---|
| 快速测试 | |
| CI环境测试 | |
| 覆盖率检查 | |
| 单个文件测试 | |
| 仅运行失败的测试 | |