Skip to content

Overview of the Nx Vitest Plugin

Vitest is a fast test runner built on Vite.

The @nx/vitest plugin adds inferred Vitest targets, a project configuration generator, and CI-friendly test splitting.

You can use Vitest with Nx without the plugin and still get task caching, task orchestration, and the project graph.

Terminal window
nx add @nx/vitest

Nx infers Vitest tasks from your Vitest or Vite config files. To confirm which targets were created, open the project details view in Nx Console or run:

Terminal window
nx show project my-app

Use the configuration generator to set up a project with Vitest:

Terminal window
nx g @nx/vitest:configuration --project=my-app

The generator accepts framework-specific options. Pass the right flags for your project type:

Terminal window
nx g @nx/vitest:configuration --project=my-react-lib --uiFramework=react

Sets up Vitest with React JSX support and jsdom test environment.

See the full configuration generator reference for all options.

Run Vitest through Nx so caching and the project graph work together.

Terminal window
nx test my-app
Terminal window
nx test my-app --watch
Terminal window
nx test my-app -- MyComponent.spec.ts
Terminal window
nx test my-app --ui
Terminal window
nx test my-app --coverage

The @nx/vitest plugin creates a target for any project that has a Vitest or Vite config file with test settings. The plugin looks for:

  • 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, open the project details view in Nx Console or run nx show project my-app.

Configure the plugin in nx.json:

nx.json
{
"plugins": [
{
"plugin": "@nx/vitest",
"options": {
"testTargetName": "test",
"ciTargetName": "test-ci",
"ciGroupName": "Unit Tests (CI)",
"testMode": "watch"
}
}
]
}
OptionTypeDefaultDescription
testTargetNamestringtestName of the inferred local target.
ciTargetNamestringnoneCreates a CI-only target used for atomized runs.
ciGroupNamestringderived from ciTargetNameDisplay name for the atomized CI group.
testModewatch or runwatchDetermines whether Nx runs vitest (watch) or vitest run (single run).

There is no single "disable" flag for inference. Use include and exclude to scope which projects each plugin instance applies to.

If you use Vitest for unit and e2e tests, configure the plugin twice with different include and exclude patterns.

nx.json
{
"plugins": [
{
"plugin": "@nx/vitest",
"exclude": ["e2e/**/*"],
"options": {
"testTargetName": "test",
"testMode": "watch"
}
},
{
"plugin": "@nx/vitest",
"include": ["e2e/**/*"],
"options": {
"testTargetName": "e2e",
"ciTargetName": "e2e-ci",
"ciGroupName": "E2E Tests (CI)",
"testMode": "run"
}
}
]
}

See Set Up CI for a complete CI configuration guide.

Terminal window
nx affected -t test

This uses the project graph to determine which projects are affected by your changes and only runs tasks for those. Read more about the benefits of nx affected.

Share test and e2e cache results across your team and CI with remote caching:

Terminal window
nx connect

For slow test suites (e.g. E2E or integration tests), set ciTargetName to split tests by file for distributed CI runs. Each test file becomes its own cacheable CI task with better caching and retries. See split e2e tasks for details.

If you use testMode: "watch" locally, use testMode: "run" for CI to keep runs deterministic.

Organize tests by feature to get better cache hits and more targeted CI runs. See the feature-based testing guide for strategies on structuring tests in a monorepo.