Vitest is a next-generation testing framework built on top of Vite, designed to provide blazing-fast test execution with native TypeScript and ESM support. Unlike Jest, which relies on transformers and often struggles with modern module formats, Vitest leverages Vite's dev server architecture to deliver near-instant hot module reloading for tests, achieving 5-28x faster execution in real-world benchmarks. It offers a Jest-compatible API, making migration straightforward, while adding modern features like browser mode, test tags, and Playwright-style fixtures. The key mental model: think of Vitest as "tests running inside Vite's transform pipeline" — your test environment understands your project's configuration natively, eliminating the configuration headaches that plague other test runners.
What This Cheat Sheet Covers
This topic spans 28 focused tables and 195 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Installation and Configuration
| Command | Example | Description |
|---|---|---|
npm install -D vitest | Installs Vitest as a dev dependency; works with Vite 3+ and Node 18+. | |
export default defineConfig({ test: { globals: true }}) | Configures Vitest via vitest.config.ts or vite.config.ts using the test property. | |
include: ['**/*.{test,spec}.ts'] | Defines glob patterns for test file discovery; defaults to **/*.{test,spec}.?(c|m)[jt]s?(x). | |
exclude: ['**/node_modules/**'] | Glob patterns to exclude from test runs; node_modules, dist, and .git excluded by default. | |
setupFiles: ['./tests/setup.ts'] | Runs specified files before each test file; useful for global test configuration and imports. |