Vitest - Modern TypeScript Testing
Overview
Vitest is a next-generation test framework powered by Vite, designed for modern TypeScript/JavaScript projects. It provides blazing-fast test execution through HMR-based test running, native ESM support, and first-class TypeScript integration.
Key Features:
- ⚡ Vite-native: Instant HMR-based test execution (10-100x faster than Jest)
- 🎯 TypeScript-first: Built-in TypeScript support, no configuration needed
- 🔄 ESM-native: Native ES modules, async/await, top-level await
- 🧪 Jest-compatible: Compatible API for easy migration
- 📸 Snapshot testing: Built-in snapshot support
- 🎨 Component testing: React Testing Library, Vue Test Utils integration
- 📊 Coverage: Built-in v8/c8 coverage (faster than Istanbul)
- 🌐 UI mode: Beautiful web UI for test debugging
Installation:
Basic Setup
1. Configure Vitest
vitest.config.ts:
2. TypeScript Configuration
tsconfig.json:
Alternative (without globals):
3. Package.json Scripts
Core Testing Patterns
Basic Test Structure
TypeScript Type Testing
Mocking and Spies
vi.mock for Module Mocking
vi.spyOn for Method Spying
Mock Implementation
Mocking Timers
React Testing Integration
Setup React Testing Library
vitest.config.ts (React):
src/test/setup.ts:
React Component Testing
Testing Hooks
Vue Testing Integration
Setup Vue Test Utils
vitest.config.ts (Vue):
Vue Component Testing
Async Testing
Testing Promises
Testing Async Functions
Snapshot Testing
Basic Snapshots
Snapshot Serializers
Coverage Configuration
Advanced Coverage Setup
vitest.config.ts:
Running Coverage
Migration from Jest
API Compatibility
Vitest provides Jest-compatible API:
Migration Checklist
1. Update Dependencies:
2. Update package.json:
3. Replace jest.config.js with vitest.config.ts:
4. Update Test Files:
Advanced Patterns
Concurrent Testing
Test Context
Custom Matchers
Best Practices
- Use globals: true - Simpler imports, Jest-compatible
- Prefer vi over jest - Use Vitest-native API for new code
- Use v8 coverage - Faster than Istanbul, works with native ESM
- Test in isolation - Each test should be independent
- Mock external dependencies - Network, file system, timers
- Use TypeScript - Full type safety in tests
- Run tests in CI mode - Use
vitest runfor CI, not watch mode - Leverage UI mode - Debug failing tests visually
- Use describe.concurrent - Parallelize independent tests
- Keep tests focused - One assertion per test when possible
Common Pitfalls
❌ Not using CI mode in CI/CD:
✅ Correct approach:
❌ Forgetting to await async tests:
❌ Not cleaning up mocks:
❌ Using wrong environment:
Resources
- Documentation: https://vitest.dev
- API Reference: https://vitest.dev/api/
- Migration Guide: https://vitest.dev/guide/migration.html
- Examples: https://github.com/vitest-dev/vitest/tree/main/examples
- UI Mode: https://vitest.dev/guide/ui.html
Related Skills
When using Vitest, consider these complementary skills:
- typescript-core: Advanced TypeScript type patterns, tsconfig, and runtime validation
- react: React component testing with Testing Library integration
- test-driven-development: Complete TDD workflow (RED/GREEN/REFACTOR cycle)
Quick TypeScript Type Patterns (Inlined for Standalone Use)
Quick React Testing Patterns (Inlined for Standalone Use)
Quick TDD Workflow Reference (Inlined for Standalone Use)
RED → GREEN → REFACTOR Cycle:
-
RED Phase: Write Failing Test
-
GREEN Phase: Make It Pass
-
REFACTOR Phase: Improve Code
Test Structure: Arrange-Act-Assert (AAA)
Vitest-Specific TDD Features:
[Full TypeScript, React, and TDD workflows available in respective skills if deployed together]
Summary
- Vitest is the modern standard for TypeScript testing
- 10-100x faster than Jest through Vite-native HMR
- ESM-first with native module support
- Jest-compatible API for easy migration
- TypeScript-first with built-in type support
- Component testing for React and Vue
- v8 coverage faster than Istanbul
- UI mode for visual test debugging
- Perfect for: Modern TypeScript projects, Vite-based apps, React/Vue components


