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

config-management.mddocs/

Configuration Management

Utilities for programmatically modifying Jest configuration files using AST-based manipulation. These functions provide safe, type-aware methods for updating Jest configurations without manual string manipulation.

Capabilities

Config Property Management

Add or remove properties from Jest configuration files with full TypeScript AST support.

/**
 * Add a property to the Jest configuration file
 * @param host - Nx virtual file system tree
 * @param path - Path to Jest configuration file
 * @param propertyName - Property name or nested path (e.g., 'transform' or ['setupFilesAfterEnv', 0])
 * @param value - Value to set for the property
 * @param options - Configuration options for value handling
 */
function addPropertyToJestConfig(
  host: Tree,
  path: string,
  propertyName: string | string[],
  value: unknown,
  options?: { valueAsString: boolean }
): void;

/**
 * Remove a property from the Jest configuration file
 * @param host - Nx virtual file system tree
 * @param path - Path to Jest configuration file
 * @param propertyName - Property name or nested path to remove
 */
function removePropertyFromJestConfig(
  host: Tree,
  path: string,
  propertyName: string | string[]
): void;

Usage Examples:

import { addPropertyToJestConfig, removePropertyFromJestConfig } from "@nx/jest";
import { Tree } from '@nx/devkit';

// Add a simple property
addPropertyToJestConfig(
  tree,
  "apps/my-app/jest.config.ts",
  "verbose",
  true
);

// Add nested configuration
addPropertyToJestConfig(
  tree,
  "apps/my-app/jest.config.ts",
  ["transform", "^.+\\.vue$"],
  "vue-jest"
);

// Add array element
addPropertyToJestConfig(
  tree,
  "apps/my-app/jest.config.ts",
  ["setupFilesAfterEnv", 0],
  "<rootDir>/src/test-setup.ts"
);

// Add raw code as string
addPropertyToJestConfig(
  tree,
  "apps/my-app/jest.config.ts",
  "testMatch",
  "['<rootDir>/src/**/*.spec.ts']",
  { valueAsString: true }
);

// Remove properties
removePropertyFromJestConfig(
  tree,
  "apps/my-app/jest.config.ts",
  "verbose"
);

// Remove nested property
removePropertyFromJestConfig(
  tree,
  "apps/my-app/jest.config.ts",
  ["moduleNameMapper", "^@/(.*)$"]
);

AST Configuration Parsing

Parse Jest configuration files as TypeScript AST objects for advanced manipulation.

/**
 * Parse Jest configuration file content into a TypeScript AST object
 * @param configString - Raw content of Jest configuration file
 * @returns TypeScript ObjectLiteralExpression AST node
 */
function jestConfigObjectAst(configString: string): ts.ObjectLiteralExpression;

/**
 * Get Jest configuration object from file content
 * @param host - Nx virtual file system tree
 * @param path - Path to Jest configuration file
 * @returns Parsed Jest configuration object
 */
function jestConfigObject(
  host: Tree,
  path: string
): Partial<Config.InitialOptions> & { [index: string]: any };

/**
 * Add import statement to Jest configuration file
 * @param host - Nx virtual file system tree
 * @param path - Path to Jest configuration file
 * @param importStatement - Import statement to add at the top of the file
 */
function addImportStatementToJestConfig(
  host: Tree,
  path: string,
  importStatement: string
): void;

Usage Examples:

import { jestConfigObjectAst, jestConfigObject, addImportStatementToJestConfig } from "@nx/jest";

// Parse configuration as AST
const configContent = tree.read("jest.config.ts", "utf-8");
const configAst = jestConfigObjectAst(configContent);

// Now you can manipulate the AST directly
console.log(configAst.properties.length); // Number of configuration properties

// Get configuration as JavaScript object
const configObject = jestConfigObject(tree, "jest.config.ts");
console.log(configObject.testEnvironment); // Access configuration values

// Add import statement to configuration file
addImportStatementToJestConfig(
  tree,
  "jest.config.ts",
  "import { customMatcher } from './test-utils';"
);

Advanced AST Manipulation

Low-level functions for direct AST manipulation of Jest configuration objects.

/**
 * Add or update a property in a TypeScript object literal AST
 * @param tree - Nx virtual file system tree
 * @param object - TypeScript ObjectLiteralExpression to modify
 * @param properties - Array of property names for nested access
 * @param value - Value to set (as string representation)
 * @param path - File path for error reporting
 */
function addOrUpdateProperty(
  tree: Tree,
  object: ts.ObjectLiteralExpression,
  properties: string[],
  value: unknown,
  path: string
): void;

/**
 * Remove a property from a TypeScript object literal AST
 * @param tree - Nx virtual file system tree
 * @param object - TypeScript ObjectLiteralExpression to modify
 * @param properties - Array of property names for nested access
 * @param path - File path for error reporting
 */
function removeProperty(
  tree: Tree,
  object: ts.ObjectLiteralExpression,
  properties: string[],
  path: string
): void;

Usage Examples:

import { addOrUpdateProperty, removeProperty, jestConfigObjectAst } from "@nx/jest";

const configContent = tree.read("jest.config.ts", "utf-8");
const configObject = jestConfigObjectAst(configContent);

// Add nested property
addOrUpdateProperty(
  tree,
  configObject,
  ["coverageThreshold", "global", "branches"],
  "80",
  "jest.config.ts"
);

// Remove nested property
removeProperty(
  tree,
  configObject,
  ["transform", "^.+\\.jsx?$"],
  "jest.config.ts"
);

Common Configuration Patterns

Adding Test Environment Configuration

// Set test environment
addPropertyToJestConfig(tree, configPath, "testEnvironment", "jsdom");

// Add test environment options
addPropertyToJestConfig(
  tree,
  configPath,
  "testEnvironmentOptions",
  {
    customExportConditions: ["node", "require"]
  }
);

Configuring Module Resolution

// Add module name mapper
addPropertyToJestConfig(
  tree,
  configPath,
  ["moduleNameMapper", "^@/(.*)$"],
  "<rootDir>/src/$1"
);

// Add module file extensions
addPropertyToJestConfig(
  tree,
  configPath,
  "moduleFileExtensions",
  ["ts", "tsx", "js", "jsx", "json"]
);

Setting Up Transformations

// Add TypeScript transformation
addPropertyToJestConfig(
  tree,
  configPath,
  ["transform", "^.+\\.ts$"],
  ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }]
);

// Add file transformation for assets
addPropertyToJestConfig(
  tree,
  configPath,
  ["transform", "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)$"],
  "jest-transform-stub"
);

Coverage Configuration

// Set coverage directory
addPropertyToJestConfig(
  tree,
  configPath,
  "coverageDirectory",
  "coverage"
);

// Configure coverage reporters
addPropertyToJestConfig(
  tree,
  configPath,
  "coverageReporters",
  ["html", "text", "lcov"]
);

// Set coverage thresholds
addPropertyToJestConfig(
  tree,
  configPath,
  "coverageThreshold",
  {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    }
  }
);

Error Handling

The configuration management functions include built-in error handling:

  • File Not Found: Throws error if Jest configuration file doesn't exist
  • Parse Errors: Logs warnings and manual update instructions for invalid syntax
  • AST Manipulation Errors: Provides detailed error messages for failed property operations
try {
  addPropertyToJestConfig(tree, "invalid/path.ts", "testMatch", ["**/*.spec.ts"]);
} catch (error) {
  console.error("Failed to update Jest config:", error.message);
  // Error: Cannot find 'invalid/path.ts' in your workspace.
}

Property Name Formats

Property names can be specified in multiple formats:

// Simple property name
addPropertyToJestConfig(tree, path, "verbose", true);

// Dot-separated nested properties  
addPropertyToJestConfig(tree, path, "coverageThreshold.global.branches", 80);

// Array of property names for complex nesting
addPropertyToJestConfig(tree, path, ["transform", "^.+\\.vue$"], "vue-jest");

// Array indices for array properties
addPropertyToJestConfig(tree, path, ["setupFilesAfterEnv", 0], "./setup.ts");

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