Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Vitest is a blazing fast testing framework powered by Vite. It provides a Jest-compatible API with modern features and instant HMR (Hot Module Replacement) for tests.
Instant test execution
Uses Vite's transform
Easy migration
Native ES modules
npm install -D vitest"test": "vitest""test:ui": "vitest --ui"// math.js
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
// math.test.js
import { describe, it, expect } from 'vitest';
import { add, multiply } from './math';
describe('Math functions', () => {
it('adds numbers correctly', () => {
expect(add(2, 3)).toBe(5);
});
it('multiplies numbers correctly', () => {
expect(multiply(4, 5)).toBe(20);
});
});
// Run: npm testimport { describe, it, expect, vi } from 'vitest';
describe('Mock functions', () => {
it('tracks function calls', () => {
const mockFn = vi.fn();
mockFn('hello');
mockFn('world');
expect(mockFn).toHaveBeenCalledTimes(2);
expect(mockFn).toHaveBeenCalledWith('hello');
});
it('mocks return values', () => {
const mockFn = vi.fn().mockReturnValue(42);
expect(mockFn()).toBe(42);
});
it('mocks resolved values', async () => {
const mockFn = vi.fn().mockResolvedValue({ data: 'success' });
const result = await mockFn();
expect(result.data).toBe('success');
});
});Run tests in real browser environment with Playwright
Test multiple projects in a monorepo setup
Built-in performance benchmarking with bench()
// Run tests with UI
// Command: npm run test:ui
// Benefits:
// - See all tests in a beautiful interface
// - Filter by file, test name, or status
// - Watch mode with instant updates
// - Click to see test details
// - View console logs and errors
// - Time tracking for each test
// vitest.config.js
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
ui: true, // Enable UI mode
open: true // Auto-open browser
}
});// math.js
export function add(a, b) {
return a + b;
}
// Tests in the same file!
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest;
it('adds numbers', () => {
expect(add(1, 2)).toBe(3);
});
it('adds negative numbers', () => {
expect(add(-1, -2)).toBe(-3);
});
}
// Benefits:
// - Tests close to implementation
// - Easy to maintain
// - No separate test files
// - Tests are tree-shaken in productionimport { bench, describe } from 'vitest';
describe('Array operations', () => {
bench('native map', () => {
const arr = Array.from({ length: 1000 }, (_, i) => i);
arr.map(x => x * 2);
});
bench('for loop', () => {
const arr = Array.from({ length: 1000 }, (_, i) => i);
const result = [];
for (let i = 0; i < arr.length; i++) {
result.push(arr[i] * 2);
}
});
bench('forEach', () => {
const arr = Array.from({ length: 1000 }, (_, i) => i);
const result = [];
arr.forEach(x => result.push(x * 2));
});
});
// Output shows:
// - Operations per second
// - Time per operation
// - Statistical analysis
// - Performance comparisonimport { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true, // Use global test, expect, etc.
environment: 'jsdom', // or 'node', 'happy-dom'
coverage: {
provider: 'v8', // or 'istanbul'
reporter: ['text', 'html'],
exclude: ['node_modules', 'dist']
},
ui: true, // Enable UI mode
watch: false // Disable watch in CI
}
});// types.ts
export interface User {
id: number;
name: string;
email: string;
}
export function createUser(name: string, email: string): User {
return {
id: Math.random(),
name,
email
};
}
// types.test.ts
import { describe, it, expect } from 'vitest';
import { createUser, type User } from './types';
describe('User creation', () => {
it('creates a valid user', () => {
const user: User = createUser('Alice', 'alice@example.com');
expect(user).toHaveProperty('id');
expect(user).toHaveProperty('name', 'Alice');
expect(user).toHaveProperty('email', 'alice@example.com');
});
it('user has correct types', () => {
const user = createUser('Bob', 'bob@example.com');
expect(typeof user.id).toBe('number');
expect(typeof user.name).toBe('string');
expect(typeof user.email).toBe('string');
});
});
// No configuration needed - TypeScript works out of the box!10x faster than Jest
Instant HMR for tests
Same API as Jest
Easy migration
Browser-based test UI
Visual test management
Native ESM, TypeScript
Vite-powered