Vitest is a blazing-fast unit testing framework powered by Vite, designed to integrate seamlessly with Vite-based projects. It leverages Vite's dev server internally to transform files during testing, ensuring consistent configuration and performance between development and test environments. Vitest is Jest-compatible, offering a familiar API for assertions (like `expect`) and mocking, alongside out-of-the-box support for ESM, TypeScript, and JSX. The current stable version is 4.1.4, with major releases occurring roughly annually (e.g., v3 in Jan 2025, v4 in Oct 2025) and frequent minor/patch updates. Its key differentiators include shared configuration with Vite, Hot Module Replacement (HMR) for tests, multithreading workers, and a comprehensive ecosystem for component testing across various frameworks like Vue, React, and Svelte. It aims to eliminate the configuration overhead often associated with using other test runners in Vite projects.
npm install vitest-dev-serverVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up Vitest for a React project with `jsdom` environment, a setup file, and basic unit tests for a utility function. It shows `defineConfig` for Vitest, a simple `sum` function, and its corresponding test file using `test`, `expect`, and `describe`.
Review the official migration guide for Vitest v4.0. Install required browser providers. Update import paths for browser-specific utilities.
Consult the Vitest v3.0 migration guide for detailed changes and adjust coverage configurations and custom environment setups accordingly.
Install and configure `vite-tsconfig-paths` as a Vite plugin in your `vite.config.ts` (or `vitest.config.ts`) to resolve `tsconfig.json` paths.
Switch Vitest's `pool` configuration to `'forks'` or `'vmForks'` in `vitest.config.ts` to avoid this issue, e.g., `test: { pool: 'forks' }`.Review your `server` configuration. For general Vite server options, ensure they are at the top level of your `vite.config.ts` (if sharing config), not nested under `test.server` unless specifically for `deps.inline` or `deps.external`.
Use the default export for such modules (`import pkg from 'some-cjs-pkg'`) or enable `deps.interopDefault: true` in your Vitest config, though this is a heuristic. For better compatibility, ensure dependencies are properly ESM-compatible or use patching if necessary.
Verify the import path. If using `baseUrl` in `tsconfig.json`, install `vite-tsconfig-paths` and add `tsconfigPaths()` to your Vite/Vitest plugins.
Configure Vitest to use `pool: 'forks'` or `pool: 'vmForks'` in your `vitest.config.ts` file.
Ensure `vi.mock` calls are at the top level of the test file before any imports of the module to be mocked. If the module is loaded in a setup file, consider using `vi.resetModules()` after the initial load or refactor to avoid early loading in the test context.
Try importing the default export (`import Y from 'Y'`) and access `X` as a property (`Y.X`), or configure `deps.interopDefault: true` in `vitest.config.ts`.
Convert `require()` calls to ES module `import` statements or dynamic `import()`. Ensure your `package.json` either has `"type": "module"` or uses `.mjs` / `.mts` file extensions for ESM files.