CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-nx--jest

Nx plugin for Jest testing with executors, generators, and configuration utilities for monorepo testing workflows.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

project-configuration.mddocs/

Project Configuration

Generators for initializing Jest in Nx workspaces and adding Jest configuration to individual projects. These generators handle dependency installation, configuration file creation, and workspace integration.

Capabilities

Configuration Generator

Adds Jest configuration to an existing project with comprehensive setup options.

/**
 * Add Jest configuration to a project
 * @param tree - Nx virtual file system tree
 * @param options - Jest project configuration options
 * @returns Promise resolving to generator callback for post-execution tasks
 */
function configurationGenerator(
  tree: Tree, 
  options: JestProjectSchema
): Promise<GeneratorCallback>;

/**
 * @deprecated Use configurationGenerator instead. Will be removed in Nx v22.
 */
const jestProjectGenerator = configurationGenerator;

Jest Project Schema

Configuration options for setting up Jest in a project.

interface JestProjectSchema {
  /** Target project name */
  project: string;
  /** Name of the test target (default: 'test') */
  targetName?: string;
  /** Enable TSX support for React projects */
  supportTsx?: boolean;
  /** Setup file configuration for specific frameworks */
  setupFile?: 'angular' | 'web-components' | 'none';
  /** Skip adding Jest serializers */
  skipSerializers?: boolean;
  /** Test environment configuration */
  testEnvironment?: 'node' | 'jsdom' | 'none';
  /** Skip formatting generated files */
  skipFormat?: boolean;
  /** Add Jest plugin to nx.json */
  addPlugin?: boolean;
  /** TypeScript compiler to use */
  compiler?: 'tsc' | 'babel' | 'swc';
  /** Skip updating package.json */
  skipPackageJson?: boolean;
  /** Generate JavaScript configuration instead of TypeScript */
  js?: boolean;
  /** Name of runtime tsconfig file */
  runtimeTsconfigFileName?: string;
  /** Internal flag for explicit target configuration */
  addExplicitTargets?: boolean;
  /** Keep existing dependency versions */
  keepExistingVersions?: boolean;
  /** @deprecated Use compiler option instead */
  babelJest?: boolean;
  /** @deprecated Use setupFile option instead */
  skipSetupFile?: boolean;
}

type NormalizedJestProjectSchema = JestProjectSchema & {
  rootProject: boolean;
  isTsSolutionSetup: boolean;
};

Usage Examples:

import { configurationGenerator } from "@nx/jest";
import { Tree } from '@nx/devkit';

// Basic Jest setup for a library
await configurationGenerator(tree, {
  project: "my-lib",
  testEnvironment: "node"
});

// React component testing setup
await configurationGenerator(tree, {
  project: "my-react-app",
  testEnvironment: "jsdom",
  supportTsx: true,
  setupFile: "none",
  compiler: "swc"
});

// Angular project setup
await configurationGenerator(tree, {
  project: "my-angular-app",
  testEnvironment: "jsdom",
  setupFile: "angular",
  supportTsx: false
});

Init Generator

Initializes Jest workspace-wide configuration and dependencies.

/**
 * Initialize the @nx/jest plugin in the workspace
 * @param tree - Nx virtual file system tree
 * @param options - Jest initialization options
 * @returns Promise resolving to generator callback
 */
function jestInitGenerator(
  tree: Tree, 
  options: JestInitSchema
): Promise<GeneratorCallback>;

/**
 * Internal implementation of jest init generator
 */
function jestInitGeneratorInternal(
  tree: Tree, 
  options: JestInitSchema
): Promise<GeneratorCallback>;

Jest Init Schema

Options for workspace-level Jest initialization.

interface JestInitSchema {
  /** Skip formatting generated files */
  skipFormat?: boolean;
  /** Skip updating package.json */
  skipPackageJson?: boolean;
  /** Keep existing dependency versions */
  keepExistingVersions?: boolean;
  /** Update package.json scripts */
  updatePackageScripts?: boolean;
  /** Add Jest plugin to nx.json */
  addPlugin?: boolean;
}

Usage Examples:

import { jestInitGenerator } from "@nx/jest";

// Initialize Jest with plugin support
await jestInitGenerator(tree, {
  addPlugin: true,
  updatePackageScripts: true
});

// Initialize Jest without modifying package.json
await jestInitGenerator(tree, {
  skipPackageJson: true,
  skipFormat: false
});

Convert to Inferred Generator

Migrates existing Jest projects to use the Nx plugin inference system.

/**
 * Convert existing Jest project(s) using @nx/jest:jest executor to use @nx/jest/plugin
 * @param tree - Nx virtual file system tree
 * @param options - Conversion options
 * @returns Promise resolving to generator callback
 */
function convertToInferredGenerator(
  tree: Tree,
  options: ConvertToInferredSchema
): Promise<GeneratorCallback>;

interface ConvertToInferredSchema {
  /** Specific project to convert (optional) */
  project?: string;
  /** Skip formatting generated files */
  skipFormat?: boolean;
}

Usage Examples:

import { convertToInferredGenerator } from "@nx/jest";

// Convert all Jest projects to use plugin inference
await convertToInferredGenerator(tree, {});

// Convert specific project
await convertToInferredGenerator(tree, {
  project: "my-app"
});

Generator Workflow

The generators follow this typical workflow:

  1. Dependency Management: Install required Jest packages and type definitions
  2. Configuration Creation: Generate Jest configuration files (jest.config.ts)
  3. TypeScript Setup: Create or update tsconfig.spec.json for test files
  4. Framework Integration: Add framework-specific setup files if needed
  5. Workspace Integration: Update nx.json and project.json as needed
  6. Plugin Configuration: Add Jest plugin to nx.json if requested

Configuration Files Generated

The generators create several configuration files:

// jest.config.ts (example output)
export default {
  displayName: 'my-project',
  preset: '../../jest.preset.js',
  testEnvironment: 'jsdom',
  transform: {
    '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
  },
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageDirectory: '../../coverage/apps/my-project',
};

// tsconfig.spec.json (example output)
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "module": "commonjs",
    "types": ["jest", "node"]
  },
  "include": [
    "jest.config.ts",
    "src/**/*.test.ts",
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}

Setup Files

Framework-specific setup files are created based on the setupFile option:

  • angular: DOM testing utilities and Angular testing setup
  • web-components: Custom elements and Web Components testing setup
  • none: No additional setup files

Install with Tessl CLI

npx tessl i tessl/npm-nx--jest

docs

config-management.md

index.md

multi-project.md

plugin-system.md

preset-config.md

project-configuration.md

test-execution.md

tile.json