Vitest
Blazing-fast unit test framework powered by Vite.
Quick Start
// sum.test.js
import { describe, it, expect } from 'vitest'
function sum(a, b) { return a + b }
describe('sum', () => {
it('adds numbers', () => {
expect(sum(1, 2)).toBe(3)
})
})
When to Use
- Vite-based projects
- Vue/React/Svelte component testing
- TypeScript-native testing
- Fast feedback development
Step-by-Step
- Install:
npm install --save-dev vitest - Add
"test": "vitest"to package.json - Write tests with Jest-compatible API
- Run:
npm test
Dependencies
npm install --save-dev vitest @vue/test-utils jsdom
Examples
import { mount } from '@vue/test-utils'
import Counter from './Counter.vue'
it('increments on click', async () => {
const wrapper = mount(Counter)
await wrapper.find('button').trigger('click')
expect(wrapper.text()).toContain('1')
})
Resources
Validation
- Tests pass with
vitest run - Watch mode works:
vitest - Coverage generates:
vitest --coverage