Vitest
Overview
Vitest is a next-generation testing framework powered by Vite that reuses your app's Vite config and transform pipeline for native ESM, TypeScript, and JSX support. Its API is largely Jest-compatible (describe/it/expect), with a fast smart-watch mode. Read this when adding tests to a Vite-based project, migrating from Jest, configuring coverage, or setting up jsdom/browser environments.
Official sources
Install / setup
npm install -D vitest
Source: https://vitest.dev/guide/ (add a "test": "vitest" script, then run npm run test).
Core concepts
- Vite-powered — reuses
vite.config plugins/resolve/aliases, so tests transform code like the app does.
- Jest-compatible API —
describe, it/test, expect, vi.fn()/vi.mock() ease migration.
- Smart watch — reruns only tests affected by changed modules via the module graph.
- Environments —
node, jsdom, or happy-dom per file via // @vitest-environment or config.
- In-source testing — colocate tests inside source using
import.meta.vitest.
- Coverage —
--coverage backed by v8 or istanbul providers.
vi utility — mocks, spies, fake timers, and module stubbing namespace.
- Browser mode — run component tests in a real browser via the experimental browser runner.
Best practices
Common pitfalls
- Forgetting
await on async expectations → use await expect(p).resolves / .rejects.
vi.mock not applied → it is hoisted; keep factory pure and import the mocked module after.
- Tests pass in Jest but fail here on ESM → rely on Vite resolution, not Jest's CJS transforms.
Examples
import { expect, test } from 'vitest';
import { sum } from './sum';
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Further reading
Related skills
- ../jest — the framework Vitest's API mirrors
- ../cypress — pairs with Vitest for E2E coverage above unit tests
- ../playwright — browser-level E2E to complement Vitest unit tests
1---2name: vitest3description: Vitest is a Vite-native, Jest-compatible testing framework with instant HMR-style watch, ESM/TS support, and built-in coverage; consult when testing Vite/Vue/React/Svelte projects, migrating from Jest, configuring vitest.config, or using in-source and browser-mode testing.4domain: stack5category: testing6tags: [vitest, vite, javascript, typescript, unit-testing, esm, vue]7official_sources:8 - https://vitest.dev/guide/9 - https://github.com/vitest-dev/vitest10 - https://www.npmjs.com/package/vitest11verified: 2026-06-1712metadata:13 source: https://skillmd.com/skills/gabriel7silva/vitest14---1516# Vitest1718## Overview19Vitest is a next-generation testing framework powered by Vite that reuses your app's Vite config and transform pipeline for native ESM, TypeScript, and JSX support. Its API is largely Jest-compatible (`describe`/`it`/`expect`), with a fast smart-watch mode. Read this when adding tests to a Vite-based project, migrating from Jest, configuring coverage, or setting up jsdom/browser environments.2021## Official sources22- Docs: https://vitest.dev/guide/23- Repo: https://github.com/vitest-dev/vitest24- Install: https://www.npmjs.com/package/vitest2526## Install / setup27```bash28npm install -D vitest29```30Source: https://vitest.dev/guide/ (add a `"test": "vitest"` script, then run `npm run test`).3132## Core concepts33- **Vite-powered** — reuses `vite.config` plugins/resolve/aliases, so tests transform code like the app does.34- **Jest-compatible API** — `describe`, `it`/`test`, `expect`, `vi.fn()`/`vi.mock()` ease migration.35- **Smart watch** — reruns only tests affected by changed modules via the module graph.36- **Environments** — `node`, `jsdom`, or `happy-dom` per file via `// @vitest-environment` or config.37- **In-source testing** — colocate tests inside source using `import.meta.vitest`.38- **Coverage** — `--coverage` backed by v8 or istanbul providers.39- **`vi` utility** — mocks, spies, fake timers, and module stubbing namespace.40- **Browser mode** — run component tests in a real browser via the experimental browser runner.4142## Best practices43- Share one config: import the app's Vite plugins so tests resolve identically (https://vitest.dev/config/).44- Pick the lightest environment per suite; default `node` is fastest (https://vitest.dev/guide/environment).45- Use `vi.mock` hoisting awareness and `vi.resetAllMocks()` between tests (https://vitest.dev/api/vi).46- Enable `globals: true` only if you want Jest-style implicit globals (https://vitest.dev/config/#globals).4748## Common pitfalls49- Forgetting `await` on async expectations → use `await expect(p).resolves` / `.rejects`.50- `vi.mock` not applied → it is hoisted; keep factory pure and import the mocked module after.51- Tests pass in Jest but fail here on ESM → rely on Vite resolution, not Jest's CJS transforms.5253## Examples54```ts55import { expect, test } from 'vitest';56import { sum } from './sum';5758test('adds 1 + 2 to equal 3', () => {59 expect(sum(1, 2)).toBe(3);60});61```6263## Further reading64- https://vitest.dev/api/ — full test and `expect` API65- https://vitest.dev/guide/migration — migrating from Jest6667## Related skills68- ../jest — the framework Vitest's API mirrors69- ../cypress — pairs with Vitest for E2E coverage above unit tests70- ../playwright — browser-level E2E to complement Vitest unit tests