What it is
Vitest is a blazing fast unit testing framework for Vite projects, designed to be highly compatible with Jest. It supports TypeScript out-of-the-box, snapshots, mocks, and a rich API for testing modern JavaScript and TypeScript applications.
Vitest allows developers to write tests using a syntax similar to Jest, including `describe`, `it/test`, `expect`, and supports mocking, snapshots, and parallel execution. It integrates with Vite for fast startup and hot-reload support.
- Licence
- MIT
- Best known for
- Reusing your Vite config, so tests and app build the same way
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Any new project, particularly on Vite — configuration is close to zero
- You want native ESM and TypeScript support without transform gymnastics
Look elsewhere when
- React Native, where Jest remains the supported path
Installation
npm install -D vitestGetting started
The smallest useful thing you can do with it, and what each part means.
import { describe, it, expect } from 'vitest';
describe('sum function', () => {
it('adds two numbers', () => {
const sum = (a, b) => a + b;
expect(sum(1, 2)).toBe(3);
});
});import { it, expect } from 'vitest';
it('resolves async value', async () => {
const fetchData = () => Promise.resolve('data');
await expect(fetchData()).resolves.toBe('data');
});Advanced usage
Where the library earns its place over a simpler alternative.
import { vi, it, expect } from 'vitest';
const myFunc = vi.fn();
myFunc('hello');
expect(myFunc).toHaveBeenCalledWith('hello');import { it, expect } from 'vitest';
it('matches snapshot', () => {
const user = { name: 'Alice', age: 25 };
expect(user).toMatchSnapshot();
});import { vi, it, expect } from 'vitest';
import * as api from './api';
vi.mock('./api');
api.fetchData.mockResolvedValue('mocked');
it('uses mocked fetchData', async () => {
const result = await api.fetchData();
expect(result).toBe('mocked');
});import { beforeEach, afterEach, it, expect } from 'vitest';
let counter = 0;
beforeEach(() => { counter = 0; });
afterEach(() => { console.log('Test completed'); });
it('increments counter', () => { counter += 1; expect(counter).toBe(1); });Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Test failed unexpectedly
- Check the expected values and ensure mocks are correctly configured.
- Async function not awaited
- Ensure asynchronous operations are awaited or return promises from test callbacks.
- Module import issues
- Verify that Vite's module resolution is correctly set up and paths are valid.
Best practices
- Use Vitest's Vite integration for instant test feedback.
- Leverage mocks and spies to isolate unit tests.
- Use snapshot testing for UI components or structured objects.
- Organize test files logically alongside source files or in a `tests` folder.
- Run tests in watch mode during development for fast feedback loops.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Vitest was created to provide a fast, modern testing solution tightly integrated with Vite's development environment. Its zero-config setup and Vite-native execution allow instant feedback during development, making it ideal for frontend projects with hot module replacement.
