vitest
Comprehensive Vitest testing framework guide with strong emphasis on Jest-to-Vitest migration. Covers automated migration using codemods, configuration setup, API differences, best practices, and troubleshooting. Use when migrating from Jest, setting up Vitest, writing tests, configuring test environments, or resolving migration issues. Primary focus is seamless Jest migration with minimal code changes.
What this skill does
<objective>
Expert guidance for migrating from Jest to Vitest and working with the Vitest testing framework. This skill focuses primarily on **automated migration from Jest** while covering setup, configuration, and best practices.
**Key benefits of Vitest over Jest:**
- 2-10x faster test startup (built on Vite and esbuild)
- Native TypeScript support without ts-jest
- Hot Module Replacement for instant re-runs
- Jest-compatible API requiring minimal code changes
- Modern ESM-first architecture
</objective>
<quick_start>
<automated_migration>
**RECOMMENDED APPROACH**: Use automated codemods for fastest migration.
**Option 1: vitest-codemod** (recommended)
```bash
# Install globally
npm install -g @vitest-codemod/jest
# Run migration on test files
vitest-codemod jest path/to/tests/**/*.test.js
# Or use npx (no installation)
npx @vitest-codemod/jest path/to/tests
```
**Option 2: Codemod.com Platform**
```bash
# Using VS Code extension
# Install "Codemod" extension from marketplace
# Right-click project → "Run Codemod" → "Jest to Vitest"
# Using CLI
npx codemod jest/vitest
```
**What codemods handle automatically:**
- ✓ Convert `jest.mock()` → `vi.mock()`
- ✓ Convert `jest.fn()` → `vi.fn()`
- ✓ Convert `jest.spyOn()` → `vi.spyOn()`
- ✓ Convert `jest.setTimeout()` → `vi.setConfig({ testTimeout })`
- ✓ Update global matchers and timer mocks
- ✓ Transform `jest.requireActual()` → `vi.importActual()`
- ✓ Update mock resets/clears/restores
</automated_migration>
<manual_migration>
**For users who need manual control or want to understand changes:**
**1. Install Vitest**
```bash
# Remove Jest
npm uninstall jest @types/jest ts-jest jest-environment-jsdom
# Install Vitest
npm install -D vitest @vitest/ui happy-dom
```
**2. Create vitest.config.ts**
```typescript
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
globals: true, // Enable globals for Jest compatibility
environment: 'happy-dom', // Faster than jsdom
setupFiles: ['./vitest.setup.ts'],
clearMocks: true,
restoreMocks: true,
},
})
```
**3. Update package.json**
```json
{
"scripts": {
"test": "vitest",
"test:ui": "vitest --ui",
"test:run": "vitest run",
"test:coverage": "vitest run --coverage"
}
}
```
**4. Update TypeScript config**
```json
{
"compilerOptions": {
"types": ["vitest/globals"]
}
}
```
**5. Update mock syntax**
```typescript
// Replace in all test files:
jest.fn → vi.fn
jest.spyOn → vi.spyOn
jest.mock → vi.mock
jest.useFakeTimers → vi.useFakeTimers
jest.clearAllMocks → vi.clearAllMocks
```
</manual_migration>
<automated_scripts>
**For comprehensive migrations with validation and rollback:**
Ready-to-run migration scripts available in `scripts/` directory:
- `quick-migrate.sh` - Fast 30-second migration for simple projects
- `comprehensive-migrate.sh` - Full-featured migration with project detection, backups, and validation
See [references/MIGRATION_SCRIPT.md](references/MIGRATION_SCRIPT.md) for usage instructions.
</automated_scripts>
</quick_start>
<critical_differences>
<module_mocking>
**Jest**: Auto-returns default export
```typescript
jest.mock('./module', () => 'hello')
```
**Vitest**: Must specify exports explicitly
```typescript
vi.mock('./module', () => ({
default: 'hello' // Explicit default export required
}))
```
</module_mocking>
<mock_reset_behavior>
**Jest**: `mockReset()` replaces with empty function returning `undefined`
**Vitest**: `mockReset()` resets to original implementation
To match Jest behavior in Vitest:
```typescript
mockFn.mockReset()
mockFn.mockImplementation(() => undefined)
```
</mock_reset_behavior>
<globals_configuration>
**Jest**: Globals enabled by default
**Vitest**: Must explicitly enable:
```typescript
export default defineConfig({
test: {
globals: true // Enable for Jest compatibility
}
})
```
Then add to `tsconfig.json`:
```json
{
"compilerOptions": {
"types": ["vitest/globals"]
}
}
```
</globals_configuration>
<auto_mocking>
**Jest**: Files in `__mocks__/` auto-load
**Vitest**: Must call `vi.mock()` explicitly, or add to `setupFiles`:
```typescript
// vitest.setup.ts
vi.mock('./path/to/module')
```
</auto_mocking>
<async_tests>
**Jest**: Supports callback style with `done()`
**Vitest**: Use async/await or Promises
```typescript
// Before (Jest)
test('async test', (done) => {
setTimeout(() => {
expect(true).toBe(true)
done()
}, 100)
})
// After (Vitest)
test('async test', async () => {
await new Promise(resolve => {
setTimeout(() => {
expect(true).toBe(true)
resolve()
}, 100)
})
})
```
</async_tests>
</critical_differences>
<common_issues>
<testing_library_cleanup>
**Problem**: Auto-cleanup doesn't run when `globals: false`
**Solution**: Manually import cleanup in setup file
```typescript
// vitest.setup.ts
import { cleanup } from '@testing-library/react'
import { afterEach } from 'vitest'
afterEach(() => {
cleanup()
})
```
</testing_library_cleanup>
<path_aliases>
**Problem**: Jest's `moduleNameMapper` not working
**Solution**: Configure in `vitest.config.ts`
```typescript
import { defineConfig } from 'vitest/config'
import path from 'path'
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@components': path.resolve(__dirname, './src/components'),
}
}
})
```
</path_aliases>
<coverage_differences>
**Problem**: Coverage numbers don't match Jest
**Solution**: Vitest uses V8 by default. For Istanbul (Jest's provider):
```bash
npm install -D @vitest/coverage-istanbul
```
```typescript
export default defineConfig({
test: {
coverage: {
provider: 'istanbul'
}
}
})
```
</coverage_differences>
<snapshot_names>
**Problem**: Test names in snapshots use `>` separator instead of spaces
```
Jest: "describe title test title"
Vitest: "describe title > test title"
```
**Solution**: Regenerate snapshots with `npm run test -u`
</snapshot_names>
</common_issues>
<best_practices>
1. **Use `happy-dom` over `jsdom`** - 2-3x faster for most use cases
2. **Enable globals for easier migration** - Set `globals: true` in config
3. **Use watch mode during development** - `npm run test` (default behavior)
4. **Leverage UI mode for debugging** - `npm run test:ui` opens browser interface
5. **Configure auto-cleanup** - Set `clearMocks: true` and `restoreMocks: true`
6. **Use workspace configuration for monorepos** - See [CONFIG.md](references/CONFIG.md)
</best_practices>
<performance_optimization>
```typescript
export default defineConfig({
test: {
environment: 'node', // or 'happy-dom' instead of 'jsdom'
maxWorkers: 4, // Increase for parallel execution
fileParallelism: true,
testTimeout: 5000,
isolate: false, // Faster but use with caution
pool: 'threads', // or 'forks' for better isolation
}
})
```
**Pool options:**
- `threads` (default) - Fast, CPU-intensive tests
- `forks` - Better isolation, more memory
- `vmThreads` - Best for TypeScript performance
</performance_optimization>
<migration_workflow>
**Recommended migration process:**
1. **Prepare**
- Ensure all Jest tests passing
- Commit working state
- Create migration branch
2. **Install dependencies**
```bash
npm install -D vitest @vitest/ui happy-dom
```
3. **Run automated codemod**
```bash
npx @vitest-codemod/jest src/**/*.test.ts
```
4. **Create configuration**
- Add `vitest.config.ts` with `globals: true`
- Update `package.json` scripts
- Update `tsconfig.json` types
5. **Run tests and fix issues**
```bash
npm run test
```
- Address failures one by one
- Check [MIGRATION.md](references/MIGRATION.md) for solutions
6. **Update CI/CD**
- Replace Jest commands with Vitest
- Update coverage paths if needed
7. **Cleanup**
```bash
npm uninstall jest @types/jest ts-jest
rm jest.config.js
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.