> ## 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.

# @nx/jest

> The Jest plugin for Nx integrates Jest unit testing into your workspace, with inferred task support and SWC transform for fast test runs.

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

```bash theme={null}
nx add @nx/jest
```

Or install manually:

<CodeGroup>
  ```bash npm theme={null}
  npm install --save-dev @nx/jest
  ```

  ```bash pnpm theme={null}
  pnpm add --save-dev @nx/jest
  ```

  ```bash yarn theme={null}
  yarn add --dev @nx/jest
  ```
</CodeGroup>

## What the plugin provides

<CardGroup cols={3}>
  <Card title="Executors" icon="play">
    The `jest` executor runs your test suite with full Nx integration: caching, affected detection, and distributed execution.
  </Card>

  <Card title="Generators" icon="wand-sparkles">
    Add Jest configuration to any existing project. Migrate executor-based projects to inferred task mode.
  </Card>

  <Card title="Inferred tasks" icon="bolt">
    Detects `jest.config.ts` (or `.js`, `.mjs`, `.cjs`) files and infers a `test` target automatically.
  </Card>
</CardGroup>

## Generators

### configuration

Add Jest configuration to an existing project. This creates a `jest.config.ts` file and wires up the test target.

```bash theme={null}
nx generate @nx/jest:configuration --project=myproject
```

Use SWC instead of `ts-jest` for faster transforms:

```bash theme={null}
nx generate @nx/jest:configuration --project=myproject --compiler=swc
```

Use Babel as the compiler:

```bash theme={null}
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`).

```bash theme={null}
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.

```json theme={null}
{
  "targets": {
    "test": {
      "executor": "@nx/jest:jest",
      "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
      "options": {
        "jestConfig": "myproject/jest.config.ts"
      },
      "configurations": {
        "ci": {
          "ci": true,
          "codeCoverage": true
        }
      }
    }
  }
}
```

Run tests:

```bash theme={null}
# 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.

```json theme={null}
{
  "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`.

<Note>
  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.
</Note>

## Configuration examples

### jest.config.ts with ts-jest

```typescript theme={null}
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`:

```typescript theme={null}
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

```typescript theme={null}
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:

```javascript theme={null}
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:

```bash theme={null}
# Test only affected projects
nx affected -t test

# Test affected projects in CI with coverage
nx affected -t test --configuration=ci
```

<Tip>
  Combine `nx affected` with Nx Cloud for distributed test execution across multiple CI agents, dramatically reducing total CI time.
</Tip>
