| name | test |
| description | Vitest testing framework - unit tests, mocking, best practices |
| metadata | {"language":"typescript","audience":"developers"} |
Overview
Vitest is a next-generation testing framework powered by Vite. Use this skill when writing tests, preferring Vitest over other testing frameworks.
Current Version: v4.x
Installation
npm install -D vitest
Requires Vite >=v6.0.0 and Node >=v20.0.0
Quickstart
Basic Test
export function sum(a: number, b: number) {
return a + b
}
import { expect, test } from 'vitest'
import { sum } from './sum'
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3)
})
Run Tests
npm test
npm run test -- --run
Configuration
vitest.config.ts
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
},
},
})
With Vite Project
Add to vite.config.ts:
import { defineConfig } from 'vite'
export default defineConfig({
test: {
globals: true,
environment: 'happy-dom',
},
})
Test Functions
describe - Test Suites
import { describe, it, expect } from 'vitest'
describe('math utils', () => {
it('should add numbers', () => {
expect(1 + 2).toBe(3)
})
it('should subtract numbers', () => {
expect(5 - 3).toBe(2)
})
})
test - Individual Tests
import { test, expect } from 'vitest'
test('example', () => {
expect(true).toBe(true)
})
it - Alias for test
import { it, expect } from 'vitest'
it('works the same as test', () => {
expect(1).toBe(1)
})
Expect Matchers
Common Matchers
expect(value).toBe(3)
expect(value).toEqual({ a: 1 })
expect(value).toBeNull()
expect(value).toBeUndefined()
expect(value).toBeTruthy()
expect(value).toBeFalsy()
expect(value).toContain(item)
expect(value).toHaveLength(3)
expect(value).toHaveProperty('key')
expect(value).toThrow()
expect(value).toMatch(/regex/)
expect(value).toMatchObject({ a: 1 })
Type Testing
import { expectTypeOf } from 'vitest'
expectTypeOf(value).toBeNumber()
expectTypeOf(value).toBeString()
expectTypeOf(value).toBeBoolean()
expectTypeOf(value).toEqualTypeOf<Expected>()
Mocking
vi.fn - Mock Functions
import { vi, expect, test } from 'vitest'
const mockFn = vi.fn()
mockFn()
const mockWithReturn = vi.fn(() => 'mocked')
mockWithReturn()
const mockWithImpl = vi.fn((x: number) => x * 2)
mockWithImpl(5)
vi.mock - Mock Modules
import { vi } from 'vitest'
vi.mock('./api', () => ({
fetchUser: vi.fn(() => Promise.resolve({ id: 1, name: 'John' }))
}))
import { fetchUser } from './api'
test('fetches user', async () => {
const user = await fetchUser()
expect(user.name).toBe('John')
})
vi.spyOn - Spy on Objects
import { vi, expect, test } from 'vitest'
const obj = { method: () => 'original' }
test('spies on method', () => {
const spy = vi.spyOn(obj, 'method')
obj.method()
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
})
Mocking Dates
import { vi, expect, test } from 'vitest'
test('mocked date', () => {
const date = new Date('2022-01-01')
vi.setSystemTime(date)
expect(new Date().getFullYear()).toBe(2022)
vi.useRealTimers()
})
Mocking Timers
import { vi, expect, test, fn } from 'vitest'
test('mock timers', async () => {
vi.useFakeTimers()
const callback = fn()
setTimeout(callback, 1000)
vi.runAllTimers()
expect(callback).toHaveBeenCalled()
vi.useRealTimers()
})
Mocking Globals
import { vi, expect, test } from 'vitest'
test('stub global', () => {
vi.stubGlobal('__VERSION__', '1.0.0')
expect(__VERSION__).toBe('1.0.0')
})
Mock import.meta.env
import { vi, expect, test } from 'vitest'
test('mock env', () => {
vi.stubEnv('VITE_API_URL', 'https://mocked.com')
expect(import.meta.env.VITE_API_URL).toBe('https://mocked.com')
})
Setup Files
Global Setup
import { beforeEach, vi } from 'vitest'
beforeEach(() => {
vi.clearAllMocks()
vi.resetAllMocks()
})
export default defineConfig({
test: {
setupFiles: ['./setup.ts'],
},
})
Test Context
import { test, expect } from 'vitest'
test('use test context', ({ task }) => {
console.log(task.name)
console.log(task.id)
})
Async Testing
Async/Await
import { test, expect, vi } from 'vitest'
test('async function', async () => {
const result = await asyncFunction()
expect(result).toBe('expected')
})
Resolves/Rejects
test('async resolves', async () => {
await expect(Promise.resolve('ok')).resolves.toBe('ok')
})
test('async rejects', async () => {
await expect(Promise.reject(new Error('err'))).rejects.toThrow('err')
})
Hooks
import { beforeAll, beforeEach, afterAll, afterEach, describe, it } from 'vitest'
beforeAll(() => {
})
beforeEach(() => {
})
afterEach(() => {
})
afterAll(() => {
})
Testing Types
import { expectTypeOf } from 'vitest'
import { assertType } from 'vitest'
expectTypeOf(add).parameters.toEqualTypeOf<[number, number]>()
expectTypeOf(add).returns.toEqualTypeOf<number>()
assertType<number>(add(1, 2))
Snapshot Testing
import { expect, test } from 'vitest'
test('snapshot', () => {
const result = { foo: 'bar' }
expect(result).toMatchSnapshot()
})
test('inline snapshot', () => {
expect('hello world').toMatchInlineSnapshot()
})
Coverage
npm run test -- --coverage
export default defineConfig({
test: {
coverage: {
provider: 'v8',
reporter: ['text', 'html', 'json'],
include: ['src/**/*.ts'],
exclude: ['src/**/*.test.ts'],
},
},
})
CLI Commands
vitest run
vitest watch
vitest --ui
vitest --coverage
vitest related file
vitest --grep "test"
Best Practices
- Use descriptive test names - Should describe the expected behavior
- Follow AAA pattern - Arrange, Act, Assert
- Test one thing per test - Each test should verify one behavior
- Use describe blocks - Group related tests
- Clear mocks after each test - Use
beforeEach with vi.clearAllMocks()
- Prefer fake timers - For testing time-dependent code
- Use TypeScript - For better type safety in tests
- Keep tests fast - Aim for <100ms per test
- Use
vi.fn() for mocks - Over inline implementations
- Mock external dependencies - APIs, databases, file system
- Aim for 100% code coverage - Write tests to cover all branches, statements, and functions whenever possible