Skip to content

Overview of the Nx Jest Plugin

Jest is a JavaScript testing framework.

The @nx/jest plugin adds inferred Jest targets, a Jest configuration generator, and CI-ready test splitting.

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

Terminal window
nx add @nx/jest

Verify the inferred test target in the project details view:

Terminal window
nx show project my-app

Generate Jest configuration for an existing project:

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

Replace my-app with your project name.

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

Terminal window
nx g @nx/jest:configuration --project=my-react-lib --supportTsx

Enables the JSX/TSX transform so Jest can process React components.

See the full configuration generator reference for all options.

Run tests:

Terminal window
nx test my-app

Watch for changes:

Terminal window
nx test my-app --watch

Run a specific file (positional argument or --testFile):

Terminal window
nx test my-app ./path/to/spec/file/user-profile.spec.ts
nx test my-app --testFile user-profile.spec.ts

Collect coverage:

Terminal window
nx test my-app --coverage

Snapshot example:

describe('userProfile', () => {
it('matches the saved snapshot', () => {
expect(renderUserProfile()).toMatchSnapshot();
});
});

Update snapshots with -u/--updateSnapshot and check snapshot files into source control.

The @nx/jest plugin infers tasks for any project with a Jest configuration file:

  • jest.config.js
  • jest.config.ts
  • jest.config.mjs
  • jest.config.mts
  • jest.config.cjs
  • jest.config.cts

Configure the plugin in nx.json:

nx.json
{
"plugins": [
{
"plugin": "@nx/jest/plugin",
"options": {
"targetName": "test"
}
}
]
}
OptionTypeDefaultDescription
targetNamestringtestName of the inferred Jest target.
ciTargetNamestringnoneCreates a CI-only target for atomized test tasks.
ciGroupNamestringnoneCustom group name for atomized tasks in Nx Cloud/UI.
disableJestRuntimebooleantrueWhen true, Nx skips creating the Jest runtime and computes inputs/outputs itself. Set to false to enable the Jest runtime.

disableJestRuntime defaults to true because the plugin treats it as enabled only when options.disableJestRuntime !== false. This reduces computation time for inferred tasks; set it to false if you need Jest runtime inference.

Use two plugin entries to separate unit and E2E Jest tasks, and enable CI splitting for E2E:

nx.json
{
"plugins": [
{
"plugin": "@nx/jest/plugin",
"exclude": ["e2e/**/*"],
"options": {
"targetName": "test"
}
},
{
"plugin": "@nx/jest/plugin",
"include": ["e2e/**/*"],
"options": {
"targetName": "e2e",
"ciTargetName": "e2e-ci"
}
}
]
}

ciTargetName enables split E2E tasks so each test file runs as its own CI task with better caching and retries. Use ciGroupName if you want a custom group label.

Open the project details view in Nx Console or run:

Terminal window
nx show project my-app

See Set Up CI for a complete CI configuration guide.

Terminal window
nx affected -t build test typecheck lint

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 build and typecheck 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.

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.

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

This is because each jest process creates workers based on system resources, running multiple projects via Nx and using jest workers will create too many processes overall causing the system to run slower than desired. Using the --runInBand flag tells jest to run in a single process. You can then leverage Nx parallelism to run multiple jest projects at once.

For large monorepos with many Jest projects, Batch mode can improve performance by batching compilation across projects into a single process.

Terminal window
NX_BATCH_MODE=true nx run-many -t test