| name | vitest-testing |
| description | Modern TypeScript/JavaScript testing with Vitest. Fast unit and integration tests, native ESM support, Vite-powered HMR, and comprehensive mocking. Use for testing TS/JS projects. |
| allowed-tools | Bash, Read, Edit, Write, Grep, Glob, TodoWrite |
| license | MIT |
Vitest Testing
Expert knowledge for testing JavaScript/TypeScript projects using Vitest - a blazingly fast testing framework powered by Vite.
Quick Start
Installation
bun add -d vitest
npm install -D vitest
Configuration
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
globals: true,
environment: 'node',
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
thresholds: { lines: 80, functions: 80, branches: 80 },
},
include: ['**/*.{test,spec}.{js,ts,jsx,tsx}'],
},
})
Running Tests
bun test
bun test --watch
bun test --run
bun test --coverage
bun test src/utils/math.test.ts
bun test --grep="calculates sum"
bun test --ui
bun test --reporter=verbose
Writing Tests
Basic Structure
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { add, subtract } from './math'
describe('Math utilities', () => {
beforeEach(() => {
})
it('adds two numbers correctly', () => {
expect(add(2, 3)).toBe(5)
})
it('subtracts two numbers correctly', () => {
expect(subtract(5, 3)).toBe(2)
})
})
Parametrized Tests
describe.each([
{ input: 2, expected: 4 },
{ input: 3, expected: 9 },
])('square function', ({ input, expected }) => {
it(`squares ${input} to ${expected}`, () => {
expect(square(input)).toBe(expected)
})
})
Assertions
expect(value).toBe(expected)
expect(value).toEqual(expected)
expect(value).toBeTruthy()
expect(value).toBeNull()
expect(value).toBeDefined()
expect(number).toBeGreaterThan(3)
expect(number).toBeCloseTo(0.3, 1)
expect(string).toMatch(/pattern/)
expect(array).toContain(item)
expect(object).toHaveProperty('key')
expect(object).toMatchObject({ a: 1 })
expect(() => throwError()).toThrow('message')
await expect(promise).resolves.toBe(value)
(promise)..()
Mocking
Function Mocks
import { vi } from 'vitest'
const mockFn = vi.fn()
mockFn.mockReturnValue(42)
mockFn.mockResolvedValue('async result')
mockFn.mockImplementation((x) => x * 2)
expect(mockFn).toHaveBeenCalled()
expect(mockFn).toHaveBeenCalledWith('arg')
Module Mocking
vi.mock('./api', () => ({
fetchUser: vi.fn(() => ({ id: 1, name: 'Test User' })),
}))
import { fetchUser } from './api'
beforeEach(() => {
vi.clearAllMocks()
})
Timers
beforeEach(() => vi.useFakeTimers())
afterEach(() => vi.restoreAllMocks())
it('advances timers', () => {
const callback = vi.fn()
setTimeout(callback, 1000)
vi.advanceTimersByTime(1000)
expect(callback).toHaveBeenCalledOnce()
})
it('mocks dates', () => {
const date = new Date('2024-01-01')
vi.setSystemTime(date)
expect(Date.now()).toBe(date.getTime())
})
Coverage
bun test --coverage
bun test --coverage --coverage.reporter=html
open coverage/index.html
bun test --coverage --coverage.thresholds.lines=90
Integration Testing
import request from 'supertest'
import { app } from './app'
describe('API endpoints', () => {
it('creates a user', async () => {
const response = await request(app)
.post('/api/users')
.send({ name: 'John' })
.expect(201)
expect(response.body).toMatchObject({
id: expect.any(Number),
name: 'John',
})
})
})
Best Practices
- One test file per source file:
math.ts → math.test.ts
- Group related tests with
describe() blocks
- Use descriptive test names
- Mock only external dependencies
- Use
concurrent tests for independent async tests
- Share expensive fixtures with
beforeAll()
- Aim for 80%+ coverage but don't chase 100%
See Also
test-quality-analysis - Detecting test smells
playwright-testing - E2E testing
mutation-testing - Validate test effectiveness