Nx plugin for Jest testing with executors, generators, and configuration utilities for monorepo testing workflows.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
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.
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;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
});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>;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
});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"
});The generators follow this typical workflow:
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"
]
}Framework-specific setup files are created based on the setupFile option:
Install with Tessl CLI
npx tessl i tessl/npm-nx--jest