The Testing Problem
Jest: slow startup, needs babel transform for TypeScript, fights with ESM. Mocha: no assertions built in. Testing Library: great but needs Jest underneath.
Vitest uses Vite's transform pipeline. Native TypeScript and ESM. 10x faster than Jest. Same API.
What Vitest Gives You
Jest-Compatible API
import { describe, it, expect } from 'vitest';
describe('math', () => {
it('adds numbers', () => {
expect(1 + 1).toBe(2);
});
it('handles arrays', () => {
expect([1, 2, 3]).toContain(2);
expect([1, 2, 3]).toHaveLength(3);
});
});
Migrating from Jest? Most tests work without changes.
Native TypeScript
interface User {
name: string;
email: string;
}
it('creates a user', () => {
const user: User = createUser('John', 'john@example.com');
expect(user.name).toBe('John');
});
No ts-jest. No @babel/preset-typescript. Just write TypeScript.
Watch Mode (Instant)
vitest # Watch mode by default
Vitest re-runs only affected tests when you save a file. Sub-second feedback.
In-Source Testing
// src/utils.ts
export function add(a: number, b: number) {
return a + b;
}
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest;
it('adds', () => expect(add(1, 2)).toBe(3));
}
Tests next to the code. Tree-shaken out of production builds.
UI Dashboard
vitest --ui
Visual test dashboard in your browser. See results, filter, re-run.
Coverage
vitest run --coverage
Built-in coverage via v8 or istanbul. No extra config.
Benchmarking
import { bench, describe } from 'vitest';
describe('sort', () => {
bench('Array.sort', () => {
[3, 1, 2].sort();
});
bench('custom sort', () => {
customSort([3, 1, 2]);
});
});
Performance
| Jest | Vitest | |
|---|---|---|
| Cold start | 5-10s | 0.3s |
| 100 tests | 15s | 1.5s |
| Watch re-run | 3-5s | 0.1s |
Quick Start
npm install -D vitest
npx vitest
Why This Matters
Slow tests mean developers skip tests. Vitest makes testing so fast that there's no excuse not to run them on every save.
Testing data pipelines? Check out my web scraping actors on Apify Store — reliable data for your test fixtures. For custom solutions, email spinov001@gmail.com.
Top comments (0)