vitest
Use when writing or configuring Vitest tests - assertions, mocking, coverage, and workspace-aware testing for TypeScript projects
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when writing or configuring Vitest tests - assertions, mocking, coverage, and workspace-aware testing for TypeScript projects
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Use when writing or editing .omg.md files - the human-first DSL for API specification that compiles to OpenAPI 3.1
Use when building CLI tools with Commander.js - commands, options, arguments, and help text for Node.js command-line applications
Use when parsing or generating Markdown following the CommonMark specification - AST structure, block/inline elements, and extensions
Use when creating GitHub Actions workflows for CI/CD - testing, building, publishing npm packages, and automating repository tasks
Use when working with JSON Schema for validation, OpenAPI schemas, type definitions, and data structure specification
Use when implementing Language Server Protocol features - diagnostics, completions, hover, go-to-definition, and editor integration
| name | vitest |
| description | Use when writing or configuring Vitest tests - assertions, mocking, coverage, and workspace-aware testing for TypeScript projects |
import { describe, it, expect, vi, beforeEach } from 'vitest';
describe('Calculator', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('adds numbers correctly', () => {
expect(add(2, 3)).toBe(5);
});
it('handles async operations', async () => {
const result = await fetchData();
expect(result).toMatchObject({ id: 1 });
});
it('mocks dependencies', () => {
const mockFn = vi.fn().mockReturnValue(42);
expect(mockFn()).toBe(42);
expect(mockFn).toHaveBeenCalledOnce();
});
});
| Assertion | Purpose |
|---|---|
toBe(value) | Strict equality (===) |
toEqual(value) | Deep equality |
toMatchObject(obj) | Partial object match |
toContain(item) | Array/string contains |
toThrow(error?) | Function throws |
toMatchSnapshot() | Snapshot testing |
toHaveBeenCalledWith() | Mock call verification |
// Mock module
vi.mock('./api', () => ({ fetchUser: vi.fn() }));
// Spy on method
const spy = vi.spyOn(object, 'method');
// Mock implementation
mockFn.mockImplementation((x) => x * 2);
mockFn.mockResolvedValue({ data: [] });
mockFn.mockRejectedValue(new Error('fail'));
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['**/*.test.ts'],
coverage: { provider: 'v8', reporter: ['text', 'html'] }
}
});
vitest - Watch modevitest run - Single runvitest run --coverage - With coveragevitest --workspace - Monorepo mode