Skip to content

Guide · Open source

How to use Vitest: install, run tests and watch mode

Updated · written with AI from the project's own documentation and checked against our catalogue data

Vitest is an open-source testing framework for JavaScript and TypeScript code, powered by Vite. It lets you write tests in a familiar style and run them with the same configuration your Vite-based app already uses. This guide explains what you need, how to run Vitest, how to write a basic test file, and how to use watch mode and coverage.

Vitestvitest-dev/vitest · ★ 17.3k · MIT

Quick start

  1. 1Check your versions. Confirm you have Node.js 22.12.0 or newer and Vite 6.4.0 or newer.
  2. 2Open your project folder. Go to the root of your JavaScript or TypeScript project in a terminal.
  3. 3Run Vitest. Run npx vitest to start the tool.
  4. 4Write a test file. Import describe, it, expect and assert from vitest and write a suite with one or more tests.
  5. 5Use watch mode. Keep Vitest running while you edit so tests rerun as files change.
  6. 6Add coverage. Turn on built-in coverage using v8 or istanbul, following the Vitest documentation.

What you need before you start Vitest

Vitest has minimum requirements. The README says it requires Vite 6.4.0 or newer and Node.js 22.12.0 or newer. Check your versions before you install it.

Vitest is aimed at JavaScript and TypeScript projects, especially apps already built with Vite. The README describes it as a tool for that ecosystem only.

  • Node.js 22.12.0 or newer
  • Vite 6.4.0 or newer
  • A JavaScript or TypeScript project

Run Vitest with npx

The README shows Vitest being run with npx. Run the command from your project folder to start it. The README's install section lists this command, and the project uses a package-manager install method.

The README also shows a plain command for running tests, which you can use from the same folder.

npx vitest

Write your first test file

Vitest uses a style that will look familiar if you have used Jest. You import helpers such as describe, it and expect from 'vitest', group related checks in a suite, and write each check as a test.

The README example uses three kinds of checks: an expect call with toEqual, an expect call using the chai style, and an assert call. It also shows a snapshot check with toMatchSnapshot.

  • describe groups tests into a named suite
  • it defines a single test
  • expect and assert check values
  • toMatchSnapshot compares output with a saved snapshot
import { assert, describe, expect, it } from 'vitest' describe('suite name', () => { it('foo', () => { expect(1 + 1).toEqual(2) expect(true).to.be.true }) it('bar', () => { assert.equal(Math.sqrt(4), 2) }) it('snapshot', () => { expect({ foo: 'bar' }).toMatchSnapshot() }) })

Use watch mode while you code

Vitest includes a watch mode that reruns tests as you change files. The README describes it as smart and instant, similar to hot module replacement for tests. Use it while you edit code so you see failures right away.

The full details are in the Vitest documentation's features guide, linked from the README.

Check code coverage

Vitest has built-in code coverage. The README says it is native and can use either v8 or istanbul as the underlying tool. Coverage shows which parts of your code your tests run.

Setup options and commands are in the Vitest documentation, so check there for how to turn coverage on.

  • Coverage provider options: v8 or istanbul
  • Coverage is listed as a built-in feature

Test components and the DOM

The README lists support for component testing with Vue, React, Svelte, Lit and Marko. It also lists JSDOM and happy-dom for DOM and browser API mocking, and a Browser Mode for running component tests in a real browser.

Vitest also supports Jest-compatible mocking, stubbing and spies, which helps when you test code that depends on other parts of your app.

  • Component testing examples exist for Vue, React and Svelte
  • JSDOM and happy-dom for DOM mocking
  • Browser Mode runs component tests in the browser

What people use it for

  • Testing functions and helpers in a Vite-based TypeScript app
  • Writing component tests for Vue, React or Svelte projects
  • Running snapshot tests to catch unexpected output changes
  • Measuring code coverage in a front-end project
  • Benchmarking code with Tinybench support

Frequently asked questions

Is Vitest free to use?

Yes. The project is licensed under the MIT License, which the README links to in its License section. You can use it in your projects under those terms.

What do I need to install Vitest?

The README says Vitest requires Node.js 22.12.0 or newer and Vite 6.4.0 or newer. It is intended for JavaScript and TypeScript projects.

How do I run Vitest?

Run npx vitest from your project folder, as shown in the README. Use watch mode to rerun tests automatically as you change files.

Is Vitest an alternative to Jest?

Vitest uses a Jest-style testing API and Jest-compatible mocking and expect APIs. The README says it is a good fit for developers who want Jest-style tests with faster watch mode. Other comparable tools it lists are Mocha, uvu and Ava.

Can I use Vitest with React, Vue or Svelte?

Yes. The README lists component testing examples for Vue, React, Svelte, Lit and Marko, and mentions Browser Mode for running component tests in the browser.

Does Vitest work with non-Vite projects?

The README describes Vitest as powered by Vite and says it uses the same configuration as a Vite-based app. Check the Vitest documentation before using it in a project that does not use Vite.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Vitest suits JavaScript and TypeScript developers who already use Vite and want Jest-style tests with fast watch mode, coverage and component testing. It is not a fit for non-developers, for projects outside JavaScript or TypeScript, or for projects that cannot meet the Node.js 22.12.0 and Vite 6.4.0 minimums, where Jest, Mocha, uvu or Ava may be better choices.

Install options & downloads