Vitest: Next-Generation Testing Framework

11.0.3 · active · verified Sun Apr 19

Vitest is a blazing-fast unit testing framework powered by Vite, designed to integrate seamlessly with Vite-based projects. It leverages Vite's dev server internally to transform files during testing, ensuring consistent configuration and performance between development and test environments. Vitest is Jest-compatible, offering a familiar API for assertions (like `expect`) and mocking, alongside out-of-the-box support for ESM, TypeScript, and JSX. The current stable version is 4.1.4, with major releases occurring roughly annually (e.g., v3 in Jan 2025, v4 in Oct 2025) and frequent minor/patch updates. Its key differentiators include shared configuration with Vite, Hot Module Replacement (HMR) for tests, multithreading workers, and a comprehensive ecosystem for component testing across various frameworks like Vue, React, and Svelte. It aims to eliminate the configuration overhead often associated with using other test runners in Vite projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up Vitest for a React project with `jsdom` environment, a setup file, and basic unit tests for a utility function. It shows `defineConfig` for Vitest, a simple `sum` function, and its corresponding test file using `test`, `expect`, and `describe`.

import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: './vitest.setup.ts',
  },
});

// vitest.setup.ts
import '@testing-library/jest-dom';

// src/sum.ts
export function sum(a: number, b: number): number {
  return a + b;
}

// src/sum.test.ts
import { expect, test } from 'vitest';
import { sum } from './sum';

describe('sum function', () => {
  test('adds 1 + 2 to equal 3', () => {
    expect(sum(1, 2)).toBe(3);
  });

  test('adds negative numbers correctly', () => {
    expect(sum(-1, -2)).toBe(-3);
  });

  test('adds zero correctly', () => {
    expect(sum(0, 5)).toBe(5);
  });
});

// package.json script
// {
//   "scripts": {
//     "test": "vitest"
//   }
// }

view raw JSON →