Overview
Vitest is a unit and integration testing framework specifically engineered to work within the Vite ecosystem. Launched in 2022, Vitest leverages Vite's fast development server and module bundling capabilities to provide a testing experience that aligns with modern web development workflows. It is characterized by its speed, primarily due to its native ESM support and integration with Vite's Hot Module Replacement (HMR) for tests, allowing developers to see test results update in real-time as code changes occur. This approach minimizes feedback loops during development, which is a common goal for testing frameworks as noted by the web.dev article on fast test feedback.
Vitest is designed for developers working on applications built with Vite, supporting a range of front-end frameworks such as React, Vue, Svelte, and Lit. While it is opinionated in its integration with Vite, it offers a testing API that is largely compatible with Jest, a widely adopted JavaScript testing framework. This compatibility can reduce the learning curve for developers transitioning from Jest to Vitest, as many existing test suites may require minimal modifications to run. It supports both JavaScript and TypeScript out-of-the-box, including features like JSX/TSX support, import aliasing, and environment variables, all inherited from Vite's configuration.
The framework is suitable for various testing scenarios, from isolated unit tests of individual functions and components to integration tests that verify the interaction between multiple parts of an application. It includes built-in mocking capabilities, snapshot testing, and coverage reporting, which are standard features in contemporary testing tools. Vitest's architecture allows for concurrent test execution and provides a watch mode that continuously re-runs tests affected by code changes. Its design prioritizes developer experience, aiming to make testing an integral and efficient part of the development cycle rather than a separate, time-consuming phase.
Key features
- Vite-native integration: Seamlessly integrates with Vite projects, sharing configuration, resolvers, and plugins for a unified development and testing environment, as described in the Vitest guide.
- Fast test runner: Leverages Vite's server architecture for quick test setup and execution, including instant Hot Module Replacement (HMR) for tests.
- Jest-compatible API: Offers an API that closely mirrors Jest's, facilitating migration for projects previously using Jest and providing a familiar experience for many JavaScript developers.
- TypeScript support: Provides first-class support for TypeScript, including type checking and JSX/TSX transformation, directly from Vite's capabilities.
- Mocking and Spies: Includes built-in utilities for mocking modules, functions, and timers, along with spies to observe function calls.
- Snapshot testing: Supports capturing and comparing snapshots of rendered components or data structures to detect unintended changes.
- Code coverage: Integrates with popular coverage reporters like c8 and Istanbul to generate detailed reports on test coverage.
- Watch mode: Automatically re-runs tests when relevant source code or test files are modified, providing immediate feedback.
- Concurrent test execution: Runs tests in parallel to maximize efficiency, especially in larger test suites.
- In-source testing: Allows tests to be written directly within the same file as the source code, enabling closer proximity between implementation and verification.
Pricing
Vitest is a free and open-source project, distributed under the MIT License. There are no licensing fees or paid tiers associated with its use. Development and maintenance are supported by community contributions and volunteers.
| Feature | Details |
|---|---|
| Licensing | MIT License |
| Cost | Free |
| Support Model | Community-driven |
| Commercial Offerings | None (as of 2026-05-07) |
For more details, refer to the official Vitest homepage.
Common integrations
- Vite: Vitest is built specifically for Vite, sharing its configuration and plugin ecosystem for a cohesive development and testing setup. Refer to the Vitest Getting Started guide for core integration details.
- UI Frameworks (React, Vue, Svelte): Integrates with testing utilities for component testing in various frameworks, such as
@testing-library/reactfor React or@vue/test-utilsfor Vue. - TypeScript: Full support for TypeScript, leveraging Vite's built-in TypeScript capabilities for type checking and compilation.
- Code Coverage Tools (c8, Istanbul): Works with C8 (V8's native code coverage tool) and Istanbul for generating detailed code coverage reports.
- IDEs (VS Code, WebStorm): Offers extensions and built-in support for running and debugging tests directly within integrated development environments.
Alternatives
- Jest: A widely used JavaScript testing framework known for its comprehensive feature set, including mocking, snapshot testing, and an assertion library.
- Karma: A test runner that launches browsers to execute JavaScript code, often paired with testing frameworks like Mocha or Jasmine for in-browser testing.
- Playwright: An end-to-end testing framework that automates web browsers for reliable cross-browser web testing, also capable of API testing.
- Mocha: A flexible JavaScript test framework that runs on Node.js and in the browser, commonly used with assertion libraries like Chai.
- Cypress: An all-in-one testing framework for web applications, focusing on end-to-end testing with a strong developer experience.
Getting started
To begin using Vitest in a new or existing Vite project, first install it as a development dependency. Then, create a test file and write a simple test case. This example demonstrates a basic unit test for a utility function.
# Install Vitest
npm install -D vitest
# or
yarn add -D vitest
# or
pnpm add -D vitest
Next, create a simple utility file, for example, src/math.ts:
// src/math.ts
export function add(a: number, b: number): number {
return a + b;
}
export function subtract(a: number, b: number): number {
return a - b;
}
Then, create a test file, typically named src/math.test.ts:
// src/math.test.ts
import { describe, expect, it } from 'vitest';
import { add, subtract } from './math';
describe('math utilities', () => {
it('should correctly add two numbers', () => {
expect(add(1, 2)).toBe(3);
expect(add(-1, 1)).toBe(0);
expect(add(0, 0)).toBe(0);
});
it('should correctly subtract two numbers', () => {
expect(subtract(5, 3)).toBe(2);
expect(subtract(3, 5)).toBe(-2);
expect(subtract(10, 0)).toBe(10);
});
});
Finally, add a script to your package.json to run Vitest:
{
"name": "my-vite-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"test": "vitest" // Add this script
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"typescript": "^5.2.2",
"vite": "^5.2.0",
"vitest": "^1.5.0" // Ensure Vitest is in devDependencies
}
}
You can then run your tests from the command line:
npm test
# or
yarn test
# or
pnpm test
This command will execute the tests and report the results. For more advanced configurations and features, consult the Vitest documentation.