vitest
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
# Install Vitest
npm install -D vitest
# Add test script to package.json
{
"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: {
// test options
},
// your existing Vite config
})
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' // or 'jsdom' for browser environment
}
})
Framework-specific setup guides:
- Next.js: See
references/guide/index.mdfor 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
// math.test.ts
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
describeblocks to group related tests - Name test files with
.test.tsor.spec.tssuffix - 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 () => {
// Runs once before all tests
await connectDatabase()
})
beforeEach(async () => {
// Runs before each test
await clearDatabase()
})
afterEach(async () => {
// Runs after each test
await cleanup()
})
afterAll(async () => {
// Runs once after all tests
await disconnectDatabase()
})
})
For complete lifecycle reference, see references/api/hooks.md.
Running Tests
# Run all tests once
vitest run
# Watch mode (default)
vitest
# Run matching pattern
vitest --testNamePattern="should add"
# UI mode
vitest --ui
# Coverage
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')
// With return value
const mocked = vi.fn().mockReturnValue('test')
expect(mocked()).toBe('test')
// With implementation
const callback = vi.fn((x) => x + 1)
expect(callback(1)).toBe(2)
Module Mocking
import { vi, expect, it } from 'vitest'
import { fetchData } from './api'
// Mock entire module
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', // or 'istanbul'
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', // or 'webdriverio'
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:
// src/math.ts
export function add(a: number, b: number) {
return a + b
}
// @ts-ignore
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'] }, () => {
// test code
})
// Run only fast tests
// vitest --tags @fast
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.jswithvitest.config.ts - Update scripts: Change
testscript to usevitest - Update imports: Replace
@jest/globalswithvitest - 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 ofjest.useFakeTimers() - Environment variables: Use
process.envdirectly
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.allandcoverage.extensions- usecoverage.includeinstead - 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:
// vitest.config.ts
export default defineConfig({
test: {
environment: 'jsdom' // for browser tests
// or 'node' for server tests
}
})
Modules not transforming:
- Check
transformModeconfiguration - 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
includeandexcludein config
See references/guide/common-errors.md for more troubleshooting.
Resources
This skill includes comprehensive Vitest documentation organized for progressive disclosure:
Documentation Indices
- Main INDEX - Complete documentation navigation
- Guide INDEX - Usage guides by topic
- API INDEX - Complete API reference
- Config INDEX - All configuration options
Key Documentation
Getting Started:
- Getting Started Guide - Installation and first test
- Features - Vitest capabilities overview
- CLI Reference - Command-line options
Core Concepts:
- Lifecycle - Test hooks and setup/teardown
- Test Context - Sharing data between tests
- Filtering - Running specific tests
Mocking:
- Mocking Overview - Mocking guide
- vi API - Mocking utilities
- mock API - Mock functions
API Reference:
- test - Test definition
- expect - Assertions and matchers
- describe - Test suites
- hooks - Lifecycle hooks
Configuration:
- Config Reference - All options
- Coverage - Coverage setup
- Browser Mode - Browser testing config
Migration:
- Migration Guide - Version migration
- Jest Comparison - Jest to Vitest
- Vitest 4.0 - Latest release notes
Advanced:
- Browser Testing - Component testing guide
- Workspace - Monorepo setup
- Performance - Optimization
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.
More from ghosttypes/ff-5mp-api-ts
typescript-best-practices
TypeScript best practices and patterns for writing type-safe, maintainable code. Use when working with TypeScript files, configuring tsconfig, defining interfaces/types, implementing error handling, writing generics, or setting up type-safe communication patterns. Includes patterns for discriminated unions, type guards, utility types, and more.
34npm-to-pnpm
Migrate projects from npm to pnpm including lockfile conversion, workspace setup, CI/CD updates, and troubleshooting. Use when converting a single package from npm to pnpm, migrating npm workspaces or monorepos, updating CI/CD pipelines for pnpm, troubleshooting issues after npm to pnpm migration, or converting package-lock.json to pnpm-lock.yaml.
17sub-agent-creator
Interactive sub-agent creation skill for Claude Code. Use when user wants to create a custom subagent or mentions needing a specialized agent for specific tasks. This skill guides the entire subagent creation process including identifier design, system prompt generation, skill/context selection, and writing properly formatted agent files to .claude/agents.
12biome
Comprehensive Biome (biomejs.dev) integration for professional TypeScript/JavaScript development. Use for linting, formatting, code quality, and flawless Biome integration into codebases. Covers installation, configuration, migration from ESLint/Prettier, all linter rules, formatter options, CLI usage, editor integration, monorepo setup, and CI/CD integration. Use when working with Biome tooling, configuring biome.json, setting up linting/formatting, migrating projects, debugging Biome issues, or implementing production-ready Biome workflows.
11eslint
Professional-grade ESLint development for JavaScript and TypeScript projects. Use when working with ESLint for (1) configuring ESLint in projects, (2) understanding or fixing ESLint errors and warnings, (3) creating or modifying ESLint rules, (4) integrating ESLint into build systems or editors, (5) migrating ESLint configurations, (6) setting up custom parsers or plugins, (7) troubleshooting ESLint issues, (8) implementing code quality standards, or (9) any task involving ESLint setup, configuration, or usage. Covers all ESLint versions with focus on v9+ flat config format.
10github-actions
Comprehensive GitHub Actions workflow authoring skill. Enables Claude to write production-grade CI/CD workflows with latest best practices, syntax, and security patterns. Use when creating or modifying GitHub Actions workflows, designing CI/CD pipelines, configuring workflow triggers/events/schedules, implementing security patterns (secrets, OIDC, attestations), creating custom actions, migrating from other CI systems, troubleshooting failed workflows, or any GitHub Actions automation task. Includes complete reference documentation extracted from official GitHub docs with source URLs for verification.
10