Vitest is a next-generation testing framework tightly integrated with Vite, offering a fast, Jest-compatible API for unit and component testing. As of version 4.1.4, it leverages Vite's blazing-fast HMR and shared configuration, making it an excellent choice for modern web projects. It features out-of-the-box support for TypeScript, JSX, and Vue/React components, along with advanced capabilities like in-source testing, snapshot testing, mocking, and parallel test execution. The project maintains an active release cadence, frequently pushing minor updates and experimental features, ensuring it stays current with ecosystem changes and user needs. Key differentiators include its speed, seamless Vite integration, and a rich ecosystem of plugins for coverage (V8, Istanbul), UI, and browser testing environments like JSDOM and Happy DOM.
npm install vitestVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic Vitest setup with `defineConfig`, writing tests with `describe`, `it`, and `expect`, performing module mocking with `vi.mock`, and creating snapshots.
Upgrade your Node.js environment to version 20, 22, or 24 LTS or newer using tools like `nvm` or your preferred package manager.
Add `environment: 'jsdom'` or `environment: 'happy-dom'` to your `vitest.config.ts` or `vite.config.ts` under the `test` option. Remember to install the respective peer dependency (`jsdom` or `happy-dom`).
Upgrade Vitest to version 4.1.2 or higher to ensure the `flatted` dependency is updated to a patched version, mitigating the CVE.
Explicitly specify the full or relative path to your `setupFiles` in `vitest.config.ts` or `vite.config.ts` to ensure they are correctly located and executed.
Place your `vi.mock('module-name', () => ({ ... }))` calls at the top of your test file, before any `import` statements for the module you are mocking. For hoisted mocks (`vi.hoisted`), follow its specific usage pattern carefully.In your `vitest.config.ts`, ensure `test: { globals: true }` is set, or explicitly import `test` and `expect` from `vitest`: `import { test, expect } from 'vitest';`Check your `tsconfig.json` `paths` and `vite.config.ts` `resolve.alias` configurations. Ensure they are correctly pointing to your source files and that Vitest is picking up the correct configuration. Sometimes a `clearCache` or rebuilding helps.
Ensure you are using `import { vi } from 'vitest'` in ESM files. If you are in a CommonJS environment, check your Node.js configuration for ESM compatibility or ensure Vitest is correctly transpiling/handling module types.Set `environment: 'jsdom'` or `environment: 'happy-dom'` in your `vitest.config.ts` under the `test` option. Remember to install `jsdom` or `happy-dom` as a peer dependency.