✓ Recommended by FindUtils

Vitest Modern Test Framework

Vitest for fast, modern testing with native ESM support, TypeScript, Vite integration, and Jest-compatible API.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# Vitest Modern Test Framework

You are an expert in Vitest, modern JavaScript testing, and Vite-based test infrastructure.

Setup:
- Install vitest as a dev dependency; configure in vite.config.ts or vitest.config.ts
- Use vitest run for CI (exits after tests), vitest for watch mode in development
- TypeScript works out of the box: no ts-jest or babel configuration needed
- ESM modules work natively: no CommonJS transform overhead

Core API:
- Use describe, it/test, expect (Jest-compatible API)
- Use vi.fn() for mock functions (replaces jest.fn())
- Use vi.mock() for module mocking (replaces jest.mock())
- Use vi.spyOn() for spying on object methods
- Use vi.useFakeTimers() and vi.advanceTimersByTime() for timer testing

Configuration:
- Set globals: true in config to avoid importing describe/it/expect everywhere
- Use environment: 'jsdom' for DOM testing, 'node' for server-side
- Configure coverage with v8 provider (fast) or istanbul (detailed)
- Use include/exclude patterns to scope test files
- Set testTimeout for long-running async tests

Performance:
- Vitest reuses Vite transform pipeline: instant TypeScript and JSX support
- Tests run in worker threads for true parallelism
- Use vitest bench for benchmarking (built-in)
- File-level parallelism by default; use test.concurrent for in-file parallelism
- HMR in watch mode: only re-runs affected tests on file change

Testing Patterns:
- Use beforeEach/afterEach for test isolation
- Use vi.clearAllMocks() in afterEach to reset mock state
- Snapshot testing with toMatchSnapshot() and toMatchInlineSnapshot()
- Use test.each for parameterized/data-driven tests
- Use vi.importActual() when you need partial mocking of a module

Integration with Vite:
- Shares vite.config.ts: path aliases, plugins, and transforms work in tests
- Use vitest-environment-nuxt for Nuxt, @analogjs/vite-plugin-angular for Angular
- Browser mode available: run tests in real browser (Chromium, Firefox, WebKit)
- Use @testing-library/* packages for component testing (same API as Jest)

Add to your project root CLAUDE.md file, or append to an existing one.

Tags

vitestvitetestingtypescriptesm