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.
Add to an existing workspace
Section titled “Add to an existing workspace”nx add @nx/jestVerify the inferred test target in the project details view:
nx show project my-appAdd Jest to a project
Section titled “Add Jest to a project”Generate Jest configuration for an existing project:
nx g @nx/jest:configuration --project=my-appReplace my-app with your project name.
The generator accepts framework-specific options. Pass the right flags for your project type:
nx g @nx/jest:configuration --project=my-react-lib --supportTsxEnables the JSX/TSX transform so Jest can process React components.
nx g @nx/jest:configuration --project=my-angular-lib --setupFile=angularConfigures jest-preset-angular and keeps Angular snapshot serializers enabled.
nx g @nx/jest:configuration --project=my-node-lib --testEnvironment=nodeUses the node test environment instead of the default jsdom.
See the full configuration generator reference for all options.
Local Development
Section titled “Local Development”Run tests:
nx test my-appWatch for changes:
nx test my-app --watchRun a specific file (positional argument or --testFile):
nx test my-app ./path/to/spec/file/user-profile.spec.tsnx test my-app --testFile user-profile.spec.tsCollect coverage:
nx test my-app --coverageSnapshot example:
describe('userProfile', () => { it('matches the saved snapshot', () => { expect(renderUserProfile()).toMatchSnapshot(); });});Update snapshots with -u/--updateSnapshot and check snapshot files into source control.
Configuration
Section titled “Configuration”Task inference
Section titled “Task inference”The @nx/jest plugin infers tasks for any project with a Jest configuration file:
jest.config.jsjest.config.tsjest.config.mjsjest.config.mtsjest.config.cjsjest.config.cts
Plugin options
Section titled “Plugin options”Configure the plugin in nx.json:
{ "plugins": [ { "plugin": "@nx/jest/plugin", "options": { "targetName": "test" } } ]}| Option | Type | Default | Description |
|---|---|---|---|
targetName | string | test | Name of the inferred Jest target. |
ciTargetName | string | none | Creates a CI-only target for atomized test tasks. |
ciGroupName | string | none | Custom group name for atomized tasks in Nx Cloud/UI. |
disableJestRuntime | boolean | true | When 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.
Unit and e2e configurations
Section titled “Unit and e2e configurations”Use two plugin entries to separate unit and E2E Jest tasks, and enable CI splitting for E2E:
{ "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.
View inferred tasks
Section titled “View inferred tasks”Open the project details view in Nx Console or run:
nx show project my-appCI Considerations
Section titled “CI Considerations”See Set Up CI for a complete CI configuration guide.
Build and Test Only What Changed
Section titled “Build and Test Only What Changed”nx affected -t build test typecheck lintThis 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.
Remote Caching
Section titled “Remote Caching”Share build and typecheck cache results across your team and CI with remote caching:
nx connectTask Splitting for Slow Tests
Section titled “Task Splitting for Slow Tests”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.
Feature-Based Testing
Section titled “Feature-Based Testing”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.
Performance with Parallelization
Section titled “Performance with Parallelization”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.
Batch Mode
Section titled “Batch Mode”For large monorepos with many Jest projects, Batch mode can improve performance by batching compilation across projects into a single process.
NX_BATCH_MODE=true nx run-many -t test