Nx plugin for Jest testing with executors, generators, and configuration utilities for monorepo testing workflows.
npx @tessl/cli install tessl/npm-nx--jest@21.4.0The Nx Jest Plugin provides comprehensive Jest testing capabilities for Nx monorepos, including test executors, configuration generators, and plugin infrastructure. It enables advanced features like affected testing, test parallelization, and intelligent caching within the Nx ecosystem.
npm install @nx/jestimport {
configurationGenerator,
jestInitGenerator,
addPropertyToJestConfig,
removePropertyFromJestConfig,
jestConfigObjectAst,
getJestProjectsAsync
} from "@nx/jest";For CommonJS:
const {
configurationGenerator,
jestInitGenerator,
addPropertyToJestConfig,
removePropertyFromJestConfig,
jestConfigObjectAst,
getJestProjectsAsync
} = require("@nx/jest");Plugin and preset imports:
import { createNodes, createNodesV2, JestPluginOptions } from "@nx/jest/plugin";
import { nxPreset } from "@nx/jest/preset";import { configurationGenerator, jestInitGenerator } from "@nx/jest";
import { Tree } from '@nx/devkit';
// Initialize Jest in workspace
await jestInitGenerator(tree, {
skipFormat: false,
addPlugin: true
});
// Add Jest configuration to specific project
await configurationGenerator(tree, {
project: "my-project",
testEnvironment: "jsdom",
supportTsx: true
});The Nx Jest Plugin is structured around several key components:
Jest test runner executor with support for batch execution, affected testing, and Nx-specific optimizations.
interface JestExecutorOptions {
codeCoverage?: boolean;
config?: string;
detectOpenHandles?: boolean;
logHeapUsage?: boolean;
detectLeaks?: boolean;
jestConfig: string;
testFile?: string;
bail?: boolean | number;
ci?: boolean;
color?: boolean;
clearCache?: boolean;
findRelatedTests?: string;
forceExit?: boolean;
json?: boolean;
maxWorkers?: number | string;
onlyChanged?: boolean;
changedSince?: string;
outputFile?: string;
passWithNoTests?: boolean;
randomize?: boolean;
runInBand?: boolean;
showConfig?: boolean;
silent?: boolean;
testNamePattern?: string;
testPathIgnorePatterns?: string[];
testPathPatterns?: string[];
colors?: boolean;
reporters?: string[];
verbose?: boolean;
coverageReporters?: string[];
coverageDirectory?: string;
testResultsProcessor?: string;
updateSnapshot?: boolean;
useStderr?: boolean;
watch?: boolean;
watchAll?: boolean;
testLocationInResults?: boolean;
testTimeout?: number;
setupFile?: string; // @deprecated
}Generators for initializing Jest in workspaces and adding Jest configuration to individual projects.
function configurationGenerator(
tree: Tree,
options: JestProjectSchema
): Promise<GeneratorCallback>;
function jestInitGenerator(
tree: Tree,
options: JestInitSchema
): Promise<GeneratorCallback>;
interface JestProjectSchema {
project: string;
targetName?: string;
supportTsx?: boolean;
setupFile?: 'angular' | 'web-components' | 'react-native' | 'react-router' | 'none';
skipSerializers?: boolean;
testEnvironment?: 'node' | 'jsdom' | 'none';
skipFormat?: boolean;
addPlugin?: boolean;
compiler?: 'tsc' | 'babel' | 'swc';
skipPackageJson?: boolean;
js?: boolean;
runtimeTsconfigFileName?: string;
addExplicitTargets?: boolean;
babelJest?: boolean; // @deprecated
skipSetupFile?: boolean; // @deprecated
keepExistingVersions?: boolean;
}
interface JestInitSchema {
skipFormat?: boolean;
skipPackageJson?: boolean;
keepExistingVersions?: boolean;
updatePackageScripts?: boolean;
addPlugin?: boolean;
}Utilities for programmatically modifying Jest configuration files with AST-based manipulation.
function addPropertyToJestConfig(
host: Tree,
path: string,
propertyName: string | string[],
value: unknown,
options?: { valueAsString: boolean }
): void;
function removePropertyFromJestConfig(
host: Tree,
path: string,
propertyName: string | string[]
): void;
function jestConfigObjectAst(configString: string): ts.ObjectLiteralExpression;Nx plugin infrastructure for automatic Jest project detection and configuration inference.
interface JestPluginOptions {
targetName?: string;
ciTargetName?: string;
ciGroupName?: string;
disableJestRuntime?: boolean;
}
function createNodes(
configFilePath: string,
options: JestPluginOptions | undefined,
context: CreateNodesContext
): CreateNodesResult;
function createNodesV2(
configFilePath: string,
options: JestPluginOptions | undefined,
context: CreateNodesContextV2
): Promise<CreateNodesResult>;Utilities for working with Jest multi-project configurations in Nx workspaces.
function getJestProjectsAsync(): Promise<string[]>;Pre-configured Jest settings optimized for Nx workspaces with TypeScript support.
interface NxJestPreset {
testMatch: string[];
resolver: string;
moduleFileExtensions: string[];
coverageReporters: string[];
transform: Record<string, [string, any]>;
testEnvironment: string;
testEnvironmentOptions: {
customExportConditions: string[];
};
}
const nxPreset: NxJestPreset;interface Tree {
// Nx Tree interface for file system operations
}
interface GeneratorCallback {
// Function returned by generators for post-execution tasks
(): Promise<void> | void;
}
interface ExecutorContext {
// Nx executor context with project and workspace information
}
interface CreateNodesContext {
// Context for plugin node creation
}
interface CreateNodesContextV2 {
// Enhanced context for plugin node creation v2
}
interface CreateNodesResult {
// Result object for created plugin nodes
}
type NormalizedJestProjectSchema = JestProjectSchema & {
rootProject: boolean;
isTsSolutionSetup: boolean;
};