A package beside a terminal window with green command lines

Adding Vitest to a project is a short job, but the details differ slightly depending on your package manager, whether the project already uses Vite, and whether your tests need a DOM. This guide covers the full path from an empty project to a passing test, explains what each command does, and points out the small decisions that save time later, such as choosing between watch mode and single-run mode and where to keep configuration.

Prerequisites

Vitest 5 requires Node.js 22.12 or newer; the package manifest also lists Node 24 and Node 26 as supported lines. Check your version with the node command and upgrade through nvm, fnm, Volta or the official installer if needed. Vite is a required peer dependency in the 6.4, 7 or 8 ranges. In a Vite project it is already installed; in a plain Node project the package manager will usually add it automatically, and you can install it explicitly if it does not.

Step 1: Install the package

From the project root, add Vitest as a development dependency. With npm the command is npm install -D vitest. For pnpm, use pnpm add -D vitest. Yarn users should install the Vite peer dependency explicitly with yarn add -D vitest vite. With Bun, use bun add -d vitest. These commands update both the project manifest and lockfile.

Step 2: Add the test script

Open package.json and add a scripts entry named test with the value vitest. Running npm test will now start Vitest in watch mode, which is the right default for local development. For continuous integration, add a second script such as test:ci with the value vitest run, which executes every test once and exits with a status code that the pipeline can read.

Step 3: Write the first test

Create a small function to test, for example a sum helper in src/sum.ts that returns the total of two numbers. Next to it, create src/sum.test.ts. Import test and expect from the vitest package, call test with a description string and a callback, and inside the callback use expect on the result with the toBe matcher. Vitest discovers files whose names end in .test or .spec followed by a JavaScript or TypeScript extension, so no configuration is needed for this layout.

If you prefer the globals style where describe, it and expect are available without imports, set globals to true in the test config and add vitest/globals to the types array of your tsconfig. Explicit imports are the default and work with every editor without extra setup.

Step 4: Run it

Run npm test. Vitest starts, prints the file it found, shows a green check with the test name and timing, and then waits for file changes. Edit the function to return the wrong value, save, and the test fails immediately with a diff of the expected and received values. Press q to quit the watcher or use the interactive keys that Vitest prints to filter by file name or test name.

Optional configuration

Many projects run fine with no config file. When you need one, there are two choices. In a Vite project, add a test block to vite.config.ts and include a triple-slash reference to vitest/config so TypeScript knows about the new field. In any other project, create vitest.config.ts that exports defineConfig from vitest/config. Common options include environment for jsdom or happy-dom, setupFiles for global test setup, include and exclude patterns, coverage settings and projects for monorepos.

Testing DOM code

By default tests execute in a Node environment where document and window do not exist. For component or browser-style tests, install jsdom or happy-dom as a dev dependency and set environment accordingly. You can also set the environment per file with a comment at the top of the test, which is useful when only a few tests need a DOM. For React, Vue or Svelte components, add the matching Testing Library package and the Vite plugin for the framework so JSX or single-file components compile.

Useful command-line flags

vitest run performs a single pass. Passing a path fragment filters to matching files, and the -t flag filters by test title. --coverage enables the coverage provider you installed. --ui opens the browser dashboard if @vitest/ui is present. --reporter selects output formats such as verbose, dot, json or junit. --project restricts a multi-project config to one project, and the shorter -p alias was added in Vitest 5.

Keeping Vitest updated

Patch releases are frequent. Updating within a major is low risk: bump the version, run the suite and read the release notes for anything that looks relevant. Moving between majors deserves more care because defaults change; the Vitest 4 to 5 guide on this site walks through the differences. Pin exact versions in CI if reproducibility matters more than freshness.

Common first-run problems

An engine warning or an immediate exit usually means Node is too old. A peer dependency conflict during install points to an incompatible Vite version. A ReferenceError about document means the environment is still Node. A watcher that seems to hang is simply waiting for changes; use vitest run when you want it to finish. Each of these has a dedicated section in the troubleshooting guide.

Organising tests as the project grows

Two conventions work well. Colocated tests sit next to the file they cover, which keeps imports short and makes it obvious when a module has no test. A separate tests folder groups everything in one place, which some teams prefer for integration tests that span several modules. Vitest supports both through its include patterns, and you can combine them. Shared helpers, factories and custom matchers belong in a setup file listed under setupFiles so every test gets them without repeated imports.

Conclusion

Installing Vitest from npm takes one command, one script entry and one test file. The runner finds tests by name, runs them in watch mode by default, and grows with the project through an optional config file, DOM environments and command-line flags. With Node.js 22.12 or newer and a compatible Vite, the setup described here gives you a fast, reliable test loop that works the same way on your machine and in CI.