Skip to content

Vitest is a fast unit test framework powered by Vite. It provides a modern testing experience with:

  • Lightning fast test execution powered by Vite
  • Native ESM, TypeScript, and JSX support out of the box
  • Jest-compatible API for easy migration
  • Smart instant watch mode
  • Component testing for UI frameworks

In any Nx workspace, you can install @nx/vitest by running the following command:

Terminal window
nx add @nx/vitest

This will install the correct version of @nx/vitest.

The @nx/vitest plugin will create a task for any project that has a Vitest configuration file present. Any of the following files will be recognized as a Vitest configuration file:

  • vitest.config.js
  • vitest.config.ts
  • vitest.config.mjs
  • vitest.config.mts
  • vitest.config.cjs
  • vitest.config.cts
  • vite.config.js (with test configuration)
  • vite.config.ts (with test configuration)
  • vite.config.mjs (with test configuration)
  • vite.config.mts (with test configuration)
  • vite.config.cjs (with test configuration)
  • vite.config.cts (with test configuration)

To view inferred tasks for a project, open the project details view in Nx Console or run nx show project my-project --web in the command line.

The @nx/vitest plugin is configured in the plugins array in nx.json.

nx.json
{
"plugins": [
{
"plugin": "@nx/vitest",
"options": {
"targetName": "test"
}
}
]
}
  • The targetName option controls the name of the inferred Vitest tasks. The default name is test.

Run the configuration generator to add Vitest to an existing project:

Terminal window
nx g @nx/vitest:configuration --project=<project-name>

Hint: You can use the --dry-run flag to see what will be generated.

Replace <project-name> with the name of the project you're wanting to add Vitest to.

The recommended way to run/debug Vitest tests is via an editor:

To run Vitest tests via Nx use:

Terminal window
nx test frontend

You can test specific files by passing a pattern:

Terminal window
nx test frontend -- HomePage.spec.ts

Using the --watch flag will run the tests in watch mode:

Terminal window
nx test frontend --watch

Vitest provides a UI for interacting with your tests. You can run tests in UI mode with:

Terminal window
nx test frontend --ui

Enable code coverage with the --coverage flag:

Terminal window
nx test frontend --coverage

By default, coverage reports will be generated in the coverage/ directory under the project root.

Typically, in CI it's recommended to use nx affected -t test --parallel=[# CPUs] for the best performance.

This allows Nx to run tests across multiple projects in parallel while leveraging caching to skip tests for unchanged projects.

Primary configurations for Vitest will be via the vitest.config.ts or vite.config.ts file that is generated for your project. Learn more about Vitest configurations.

The Nx task options can be configured via the project config file or via the command line flags.

If you're using inferred tasks, you can provide the Vitest args for the command you're running.

If you're currently using @nx/vite for Vitest testing, see the migration guide for instructions on switching to @nx/vitest.