vitest
Assists with unit and integration testing using Vitest, a Vite-native test runner. Use when writing tests, configuring mocks, setting up coverage, or migrating from Jest. Trigger words: vitest, unit testing, test runner, vi.fn, vi.mock, test coverage, jest replacement.
Usage
Getting Started
- Install the skill using the command above
- Open your AI coding agent (Claude Code, Codex, Gemini CLI, or Cursor)
- Reference the skill in your prompt
- The AI will use the skill's capabilities automatically
Example Prompts
- "Review the open pull requests and summarize what needs attention"
- "Generate a changelog from the last 20 commits on the main branch"
Documentation
Overview
Vitest is a Vite-native test runner that provides a Jest-compatible API with native ESM support, instant watch mode via Vite's HMR, and shared Vite configuration for aliases, plugins, and transforms. It serves as a drop-in Jest replacement with significantly faster startup and execution.
Instructions
- When writing tests, use
describe()blocks to group related tests,it()ortest()for individual cases, and follow the pattern of naming tests as behavior descriptions (e.g., "should return 404 when user not found"). - When mocking, use
vi.fn()for function mocks,vi.mock("./module")for module mocks,vi.useFakeTimers()for timer control, andvi.setSystemTime()for date mocking. - When setting up assertions, use
toBe()for primitives,toEqual()for objects/arrays, andtoMatchInlineSnapshot()for small expected outputs. - When configuring, define test settings in
vite.config.tsunder thetestproperty or in a separatevitest.config.ts, choosing the appropriate environment (jsdom,happy-dom,node). - When measuring coverage, use
@vitest/coverage-v8withvitest --coverageand set minimum thresholds in CI with--coverage.thresholds.lines=80. - When testing in browsers, use
@vitest/browserwith Playwright or WebDriverIO providers for real DOM testing instead of jsdom simulation. - When working in monorepos, use
vitest.workspace.tsfor multi-project configuration that shares common settings.
Examples
Example 1: Test a service with API mocking
User request: "Write Vitest tests for a user service that calls an external API"
Actions:
- Mock the HTTP module with
vi.mock()to intercept API calls - Write tests for success, error, and edge cases using
describeandit - Assert responses with
toEqual()and error handling withtoThrow() - Use
beforeEachwithvi.clearAllMocks()for test isolation
Output: Isolated unit tests for the user service with mocked external dependencies.
Example 2: Migrate from Jest to Vitest
User request: "Switch our test suite from Jest to Vitest"
Actions:
- Install
vitestand add test config tovite.config.ts - Replace
jest.fn()withvi.fn()andjest.mock()withvi.mock() - Update
package.jsonscripts to usevitestandvitest --coverage - Remove
ts-jest,babel-jest, and Jest config files
Output: A Vitest-powered test suite with the same tests running faster with native ESM support.
Guidelines
- Use
describeblocks to group related tests; keep individual tests focused on one behavior. - Prefer
toEqual()for objects/arrays andtoBe()for primitives. - Mock external dependencies (HTTP, database), not internal modules; test real integration where possible.
- Use
beforeEachfor test isolation, notbeforeAll; shared state between tests causes flaky results. - Name tests as behavior descriptions: "should return 404 when user not found", not "test getUserById".
- Use inline snapshots for small expected outputs; file snapshots for large/complex structures.
- Run
vitest --coveragein CI with a minimum threshold:--coverage.thresholds.lines=80.
Information
- Version
- 1.0.0
- Author
- terminal-skills
- Category
- Development
- License
- Apache-2.0