| name | vitest |
| description | Use when the user asks to write, run, or debug tests using Vitest, asks about vitest.config.ts or vite.config.ts test configuration, asks how to mock modules, functions, timers, or globals with the `vi` utility, asks about test coverage setup or thresholds, references Vitest API (describe, it, test, expect, beforeEach, afterAll, etc.), or encounters failing Vitest tests and needs help diagnosing them.
|
Vitest Skill
Contents
Quick Start
Minimum setup:
npm install -D vitest
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
environment: 'node',
},
})
First test (src/math.test.ts):
import { describe, it, expect } from 'vitest'
import { add } from './math'
describe('add', () => {
it('returns the sum of two numbers', () => {
expect(add(1, 2)).toBe(3)
})
})
npx vitest
npx vitest run
When This Skill Activates
- User asks to write a test for a function, component, or module
- User asks how to configure Vitest or integrate it into an existing Vite project
- User's tests are failing and they need help diagnosing the error
- User asks about mocking, spying, or stubbing in Vitest
- User wants to measure or enforce test coverage
Behavior Guidelines
- Prefer Vitest-native imports — import
describe, it, expect, vi, etc. from 'vitest', not from @jest/* packages.
- Respect the existing config — read
vitest.config.ts or vite.config.ts before suggesting configuration changes.
- Match the project's test style — check existing test files for naming conventions (
*.test.ts vs *.spec.ts, it vs test) and follow them.
- Use
vi for all mocking — never suggest jest.fn(), jest.mock(), etc.
- Run tests to verify — after writing or modifying tests, suggest running
npx vitest run to confirm they pass.
References Loading Guide
Load additional context files based on what the user needs:
| Situation | Load |
|---|
Questions about vitest.config.ts, environments, globals, setupFiles | references/config.md |
Questions about describe, it, test, expect, hooks | references/api.md |
Questions about vi.fn(), vi.mock(), vi.spyOn(), timers, globals | references/mocking.md |
Questions about coverage providers, thresholds, --coverage flag | references/coverage.md |
Read only what is relevant to the current question. Do not preload all files unless the user's question spans multiple areas.