Vitest
Vite-native unit test runner with instant watch mode, native ESM and TypeScript support, and Jest-compatible assertions and mocks.
Official Documentation ↗5.1.0. Check the official docs for the latest API surface.Installation
npm install -D vitestpnpm add -D vitestyarn add -D vitestMethods
No methods match this filter.
Vitest runs tests inside a Vite pipeline, so the code under test uses the same transform, alias, and TypeScript configuration as the application. Assertions and mocking are Jest-compatible, but modules are loaded through native ESM instead of a custom runtime.
Overview
Vitest is a test runner for JavaScript and TypeScript projects. It provides watch mode with instant re-runs, per-file isolation, coverage through v8 or Istanbul, browser mode, and project configuration for monorepos. Because it reuses Vite, path aliases and framework plugins usually work without a second build configuration.
Installation
Install the runner as a development dependency and add a test script to package.json. The default config file is vitest.config.ts, which can extend the existing vite.config.ts through mergeConfig. Node environments need no extra setup; component tests need the jsdom or happy-dom environment package.
Core API walkthrough
The five methods below cover the normal workflow. defineConfig types the test block and keeps Vite transforms available. describe groups cases while it declares one case per behavior. expect exposes matchers including resolves and rejects for promises. vi.fn creates a spy with recorded calls, and vi.mock replaces a module for a whole file with a factory.
Pitfalls
vi.mock calls are hoisted above imports, so factories cannot reference top-level variables unless they are declared with vi.hoisted. Fake timers must be restored, otherwise later cases hang on timers they never advance. Browser APIs are absent under the default node environment. Finally, mocks persist within a file unless clearMocks or restoreMocks is enabled.
Verification
Run npx vitest run in CI to execute once and exit non-zero on failure. Add --coverage and enforce thresholds so untested branches fail the gate. Use --reporter=verbose locally to confirm each case actually ran, and keep a failing test in the suite to prove the runner reports failures rather than silently passing.
FAQ
- Does Vitest replace Jest for every project?
- Vitest is a strong fit for Vite, Vue, Svelte, and modern TypeScript projects. Projects locked to older Jest transforms, jsdom-only APIs, or a large ecosystem of Jest-specific plugins should stay on Jest.
- How do I run a single test file?
- Use npx vitest run src/sum.test.ts to execute once, or npx vitest src/sum.test.ts to stay in watch mode. Filter by test name with the -t flag.
- Why do mocks leak between tests?
- Mocks are scoped to a test file but persist across cases in that file. Enable clearMocks, mockReset, or restoreMocks in the config, or call vi.restoreAllMocks in afterEach.
Report a problem with this API docOpens a pre-filled issue on GitHub.