Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/nrwl/nx/llms.txt

Use this file to discover all available pages before exploring further.

The @nx/jest plugin integrates Jest into your Nx workspace. It provides the jest executor for running unit tests, infers test targets from jest.config.ts files automatically, and supports SWC for faster TypeScript transforms.

Installation

nx add @nx/jest
Or install manually:
npm install --save-dev @nx/jest

What the plugin provides

Executors

The jest executor runs your test suite with full Nx integration: caching, affected detection, and distributed execution.

Generators

Add Jest configuration to any existing project. Migrate executor-based projects to inferred task mode.

Inferred tasks

Detects jest.config.ts (or .js, .mjs, .cjs) files and infers a test target automatically.

Generators

configuration

Add Jest configuration to an existing project. This creates a jest.config.ts file and wires up the test target.
nx generate @nx/jest:configuration --project=myproject
Use SWC instead of ts-jest for faster transforms:
nx generate @nx/jest:configuration --project=myproject --compiler=swc
Use Babel as the compiler:
nx generate @nx/jest:configuration --project=myproject --compiler=babel

convert-to-inferred

Migrate existing projects that use the @nx/jest:jest executor to use the inferred task plugin (@nx/jest/plugin).
nx generate @nx/jest:convert-to-inferred

# Migrate a single project
nx generate @nx/jest:convert-to-inferred --project=myproject

Executors

jest

Run Jest unit tests for a project. Supports all standard Jest CLI flags and adds Nx caching.
{
  "targets": {
    "test": {
      "executor": "@nx/jest:jest",
      "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
      "options": {
        "jestConfig": "myproject/jest.config.ts"
      },
      "configurations": {
        "ci": {
          "ci": true,
          "codeCoverage": true
        }
      }
    }
  }
}
Run tests:
# Run all tests
nx test myproject

# Run in watch mode
nx test myproject --watch

# Run with coverage
nx test myproject --codeCoverage

# Run only tests matching a pattern
nx test myproject --testPathPattern=button

# CI mode (no watch, with coverage)
nx test myproject --configuration=ci

Inferred tasks

When @nx/jest/plugin is registered in nx.json, Nx detects jest.config.ts (or .js, .mjs, .cjs, .cts, .mts) files and automatically infers a test target for each project that has one.
{
  "plugins": [
    {
      "plugin": "@nx/jest/plugin",
      "options": {
        "targetName": "test"
      }
    }
  ]
}
With inferred tasks, removing the test target from project.json entirely is safe. Nx reads the Jest configuration directly from jest.config.ts.
Inferred task caching is based on the inputs defined in your jest.config.ts and the project’s source files. Use nx show project myproject to inspect the inferred targets.

Configuration examples

jest.config.ts with ts-jest

export default {
  displayName: 'myproject',
  preset: '../../jest.preset.js',
  testEnvironment: 'node',
  transform: {
    '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
  },
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageDirectory: '../../coverage/myproject',
};

jest.config.ts with SWC

SWC provides significantly faster TypeScript transforms compared to ts-jest:
export default {
  displayName: 'myproject',
  preset: '../../jest.preset.js',
  testEnvironment: 'node',
  transform: {
    '^.+\\.[tj]s$': '@swc/jest',
  },
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageDirectory: '../../coverage/myproject',
};

jest.config.ts for React components

export default {
  displayName: 'myapp',
  preset: '../../jest.preset.js',
  testEnvironment: 'jsdom',
  transform: {
    '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
    '^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/react/babel'] }],
  },
  moduleNameMapper: {
    '\\.(css|less|scss|sass)$': 'identity-obj-proxy',
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/myapp',
  setupFilesAfterFramework: ['<rootDir>/src/test-setup.ts'],
};

Workspace-level jest.preset.js

Shared preset used by all projects:
const nxPreset = require('@nx/jest/preset').default;

module.exports = { ...nxPreset };

Running affected tests

One of Nx’s most powerful features is running only the tests affected by recent changes:
# Test only affected projects
nx affected -t test

# Test affected projects in CI with coverage
nx affected -t test --configuration=ci
Combine nx affected with Nx Cloud for distributed test execution across multiple CI agents, dramatically reducing total CI time.