| name | vitest |
| description | Comprehensive Vitest testing framework skill for writing, configuring, and running tests in JavaScript/TypeScript projects. Use when Claude needs to: set up Vitest in a new or existing project (Vite or non-Vite), write or modify tests using Vitest APIs, configure Vitest for specific scenarios (coverage, browser testing, mocking, etc.), migrate from Jest or older Vitest versions, debug test failures or configuration issues, implement advanced testing patterns (workspace, browser mode, snapshots, mocking). |
Vitest
Overview
Vitest is a blazing-fast unit test framework powered by Vite. This skill provides complete Vitest documentation, APIs, configuration options, and migration guides to enable flawless test development in any JavaScript or TypeScript project.
Key capabilities:
- Vite-native for instant feedback and HMR
- Jest-compatible API for easy migration
- Works in Vite and non-Vite projects
- Built-in TypeScript, JSX, and ESM support
- Native code coverage (V8 and Istanbul)
- Browser mode for component testing
- Workspace support for monorepos
Quick Start Decision Tree
Is this a new project or adding tests to an existing project?
- New project: Start with "Installation & Setup"
- Existing project: Check framework type below
What type of project is it?
- Vite/Vue/React/Svelte: Use Vite integration (see "Vite Project Setup")
- Non-Vite (Next.js, Angular, vanilla): Use standalone mode (see "Standalone Project Setup")
- Monorepo: Use workspace configuration (see "Workspace Setup")
Are you migrating from Jest?
- Yes: See "Migration from Jest" section
Installation & Setup
Basic Installation
npm install -D vitest
{
"scripts": {
"test": "vitest",
"test:ui": "vitest --ui"
}
}
Vite Project Setup
For projects already using Vite, extend the existing vite.config.ts:
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
},
})
Vitest automatically shares your Vite config, plugins, and transformations.
Standalone Project Setup
For non-Vite projects, create vitest.config.ts:
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: ['**/*.{test,spec}.{js,ts}'],
environment: 'node'
}
})
Framework-specific setup guides:
- Next.js: See
references/guide/index.md for configuration
- Angular: Use
environment: 'jsdom' with appropriate transformers
- React/Vue/Svelte: Extend Vite config for best experience
TypeScript Setup
Vitest supports TypeScript out of the box. Ensure tsconfig.json includes test files:
{
"include": ["src/**/*", "**/*.test.ts"]
}
Writing Tests
Basic Test Structure
import { describe, it, expect } from 'vitest'
import { add } from './math'
describe('add', () => {
it('should add two numbers', () => {
expect(add(1, 2)).toBe(3)
})
it('should handle negative numbers', () => {
expect(add(-1, -2)).toBe(-3)
})
})
Test Organization
- Use
describe blocks to group related tests
- Name test files with
.test.ts or .spec.ts suffix
- Co-locate tests with source code or in
__tests__ directories
- Use test context for sharing data between tests (see
references/guide/test-context.md)
Lifecycle Hooks
import { beforeAll, beforeEach, afterAll, afterEach } from 'vitest'
describe('database tests', () => {
beforeAll(async () => {
await connectDatabase()
})
beforeEach(async () => {
await clearDatabase()
})
afterEach(async () => {
await cleanup()
})
afterAll(async () => {
await disconnectDatabase()
})
})
For complete lifecycle reference, see references/api/hooks.md.
Running Tests
vitest run
vitest
vitest --testNamePattern="should add"
vitest --ui
vitest --coverage
See references/guide/cli.md for complete CLI reference.
Mocking
Vitest provides comprehensive mocking capabilities through the vi utility.
Function Mocking
import { vi, describe, it, expect } from 'vitest'
const mockFn = vi.fn()
mockFn('hello')
expect(mockFn).toHaveBeenCalledWith('hello')
const mocked = vi.fn().mockReturnValue('test')
expect(mocked()).toBe('test')
const callback = vi.fn((x) => x + 1)
expect(callback(1)).toBe(2)
Module Mocking
import { vi, expect, it } from 'vitest'
import { fetchData } from './api'
vi.mock('./api', () => ({
fetchData: vi.fn(() => Promise.resolve('mocked data'))
}))
it('uses mocked API', async () => {
const data = await fetchData()
expect(data).toBe('mocked data')
})
Timer Mocking
import { vi, beforeEach, expect, it } from 'vitest'
beforeEach(() => {
vi.useFakeTimers()
})
it('calls callback after timeout', () => {
const callback = vi.fn()
setTimeout(callback, 1000)
vi.advanceTimersByTime(1000)
expect(callback).toHaveBeenCalled()
})
Complete mocking reference:
- Functions:
references/guide/mocking/functions.md
- Modules:
references/guide/mocking/modules.md
- Timers:
references/guide/mocking/timers.md
- Globals:
references/guide/mocking/globals.md
- Dates:
references/guide/mocking/dates.md
Configuration Patterns
Common Configuration Scenarios
1. Browser Environment (React/Vue/Svelte)
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
environment: 'jsdom',
globals: true,
setupFiles: ['./src/test/setup.ts']
}
})
2. Code Coverage
export default defineConfig({
test: {
coverage: {
provider: 'v8',
include: ['src/**/*.{js,ts}'],
exclude: ['src/**/*.test.{js,ts}', 'src/**/*.config.{js,ts}']
}
}
})
3. Monorepo/Workspace
export default defineConfig({
test: {
workspace: [
'packages/*',
'apps/*'
]
}
})
4. Browser Mode (Component Testing)
export default defineConfig({
test: {
browser: {
enabled: true,
provider: 'playwright',
headless: true
}
}
})
See references/config/INDEX.md for complete configuration reference.
Advanced Features
Snapshot Testing
import { expect, it } from 'vitest'
it('matches snapshot', () => {
const data = { foo: 'bar' }
expect(data).toMatchSnapshot()
})
See references/guide/snapshot.md for complete snapshot guide.
In-Source Testing
Write tests directly next to code:
export function add(a: number, b: number) {
return a + b
}
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest
it('adds numbers', () => {
expect(add(1, 2)).toBe(3)
})
}
See references/guide/in-source.md for configuration.
Test Tags
Organize and filter tests by tags:
import { test } from 'vitest'
test('slow integration test', { tags: ['@slow', '@integration'] }, () => {
})
See references/guide/test-tags.md for usage.
Browser Testing
Test components in real browser:
import { expect, test } from 'vitest'
import { render } from '@testing-library/vue'
test('renders button', async () => {
const { getByText } = render(Button, {
props: { label: 'Click me' }
})
expect(getByText('Click me')).toBeTruthy()
})
See references/guide/browser/ for complete browser testing guide.
Migration
From Jest to Vitest
Vitest is largely compatible with Jest. The migration process:
- Install Vitest:
npm install -D vitest
- Update configuration: Replace
jest.config.js with vitest.config.ts
- Update scripts: Change
test script to use vitest
- Update imports: Replace
@jest/globals with vitest
- Verify mocks: Most Jest mocks work unchanged
Key differences:
- Auto-mocked modules: Vitest doesn't auto-mock by default
- Timer mocks: Use
vi.useFakeTimers() instead of jest.useFakeTimers()
- Environment variables: Use
process.env directly
See references/guide/comparisons.md for detailed Jest comparison.
From Older Vitest Versions
Migrating to Vitest 4.0:
- Coverage provider changes: V8 now uses AST-based remapping
- Removed
coverage.all and coverage.extensions - use coverage.include instead
- Coverage ignore hints updated - see
references/guide/migration.md
See migration guides:
- Vitest 4.0:
references/guide/migration.md#vitest-4
- Vitest 3.0:
references/vitest-3.md
- Vitest 3.2:
references/vitest-3-2.md
Troubleshooting
Common Issues
Tests run in wrong environment:
export default defineConfig({
test: {
environment: 'jsdom'
}
})
Modules not transforming:
- Check
transformMode configuration
- Ensure dependencies are in
deps.interopDefault
Timeouts:
- Increase timeout:
test({ timeout: 10000 }, () => { ... })
- Or globally:
test: { timeout: 10000 }
Watch mode not detecting changes:
- Check file inclusion patterns
- Verify
include and exclude in config
See references/guide/common-errors.md for more troubleshooting.
Resources
This skill includes comprehensive Vitest documentation organized for progressive disclosure:
Documentation Indices
Key Documentation
Getting Started:
Core Concepts:
Mocking:
API Reference:
Configuration:
Migration:
Advanced:
When working with Vitest, consult the appropriate reference file based on your task. Start with the guide for conceptual understanding, then refer to API/config references for specific implementation details.