or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-jest-resolve-dependencies

Dependency resolution utilities for Jest testing framework

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/jest-resolve-dependencies@30.1.x

To install, run

npx @tessl/cli install tessl/npm-jest-resolve-dependencies@30.1.0

index.mddocs/

Jest Resolve Dependencies

Jest Resolve Dependencies provides dependency resolution utilities specifically designed for the Jest testing framework. It exports a DependencyResolver class that handles module dependency analysis, including resolving direct dependencies of modules and retrieving transitive inverse dependencies for intelligent test re-execution.

Package Information

  • Package Name: jest-resolve-dependencies
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install jest-resolve-dependencies

Peer Dependencies

This package requires additional Jest packages for full functionality:

npm install jest-haste-map jest-resolve jest-snapshot

Note: This package is primarily designed for internal use by Jest. Most applications won't need to use it directly as Jest handles dependency resolution automatically.

Core Imports

import { DependencyResolver, type ResolvedModule } from "jest-resolve-dependencies";
import type { IHasteFS } from "jest-haste-map";
import type Resolver, { ResolveModuleConfig } from "jest-resolve";
import { buildSnapshotResolver, type SnapshotResolver } from "jest-snapshot";

For CommonJS:

const { DependencyResolver } = require("jest-resolve-dependencies");
const { buildSnapshotResolver } = require("jest-snapshot");

Basic Usage

Important: This package is primarily used internally by Jest. The examples below show how to use it directly, but most developers will interact with it through Jest's testing framework rather than directly.

import { DependencyResolver } from "jest-resolve-dependencies";
import type { IHasteFS } from "jest-haste-map";
import type Resolver from "jest-resolve";
import { buildSnapshotResolver } from "jest-snapshot";

// Create resolver instance (typically created by Jest runtime, not manually)
const dependencyResolver = new DependencyResolver(
  resolver,        // Resolver instance from jest-resolve
  hasteFS,         // IHasteFS instance from jest-haste-map  
  snapshotResolver // SnapshotResolver instance from jest-snapshot
);

// Resolve direct dependencies of a file
const dependencies = dependencyResolver.resolve("/path/to/module.js");
console.log(dependencies); // Array of resolved dependency paths

// Find files that depend on changed files (for watch mode)
const changedFiles = new Set(["/path/to/changed-file.js"]);
const affectedTests = dependencyResolver.resolveInverse(
  changedFiles,
  (file) => file.endsWith('.test.js') // Filter for test files
);
console.log(affectedTests); // Array of test files that need to re-run

Architecture

Jest Resolve Dependencies is built around key dependency resolution concepts:

  • Direct Resolution: Finding immediate dependencies of a module using Jest's Haste file system
  • Mock Resolution: Handling Jest mock modules alongside regular dependencies
  • Inverse Resolution: Building dependency graphs to determine which files are affected by changes
  • Snapshot Integration: Special handling for Jest snapshot files and their associated test files
  • Transitive Analysis: Computing complete dependency chains for impact analysis

Capabilities

Module Dependency Resolution

Resolves direct dependencies of a given file, including both regular modules and Jest mock modules.

/**
 * Resolves direct dependencies of a given file
 * @param file - Absolute path to the file to resolve dependencies for
 * @param options - Optional module resolution configuration
 * @returns Array of resolved dependency file paths
 */
resolve(file: string, options?: ResolveModuleConfig): Array<string>;

Inverse Dependency Analysis

Determines which files depend on a set of changed files, essential for Jest's watch mode functionality.

/**
 * Resolves inverse dependencies returning only file paths
 * @param paths - Set of file paths that have changed
 * @param filter - Function to filter which files to include in results
 * @param options - Optional module resolution configuration
 * @returns Array of file paths that depend on the changed files
 */
resolveInverse(
  paths: Set<string>,
  filter: (file: string) => boolean,
  options?: ResolveModuleConfig
): Array<string>;

Detailed Inverse Dependency Mapping

Provides detailed module information including both files and their dependencies for comprehensive analysis.

/**
 * Resolves inverse dependencies with detailed module information
 * @param paths - Set of file paths that have changed
 * @param filter - Function to filter which files to include in results
 * @param options - Optional module resolution configuration
 * @returns Array of resolved modules with dependencies
 */
resolveInverseModuleMap(
  paths: Set<string>,
  filter: (file: string) => boolean,
  options?: ResolveModuleConfig
): Array<ResolvedModule>;

Types

/**
 * Represents a resolved module with its file path and dependencies
 */
type ResolvedModule = {
  /** Absolute file path of the module */
  file: string;
  /** Array of resolved dependency file paths */
  dependencies: Array<string>;
};

/**
 * Main class for dependency resolution in Jest
 */
class DependencyResolver {
  /**
   * Creates a new DependencyResolver instance
   * @param resolver - Jest resolver instance for module resolution
   * @param hasteFS - Jest Haste file system interface
   * @param snapshotResolver - Jest snapshot resolver instance
   */
  constructor(
    resolver: Resolver,
    hasteFS: IHasteFS,
    snapshotResolver: SnapshotResolver
  );

  resolve(file: string, options?: ResolveModuleConfig): Array<string>;
  
  resolveInverse(
    paths: Set<string>,
    filter: (file: string) => boolean,
    options?: ResolveModuleConfig
  ): Array<string>;
  
  resolveInverseModuleMap(
    paths: Set<string>,
    filter: (file: string) => boolean,
    options?: ResolveModuleConfig
  ): Array<ResolvedModule>;
}

/**
 * Module resolution configuration options (from jest-resolve)
 */
interface ResolveModuleConfig {
  conditions?: Array<string>;
  [key: string]: any;
}

Usage Examples

Finding Test Files Affected by Changes

import { DependencyResolver } from "jest-resolve-dependencies";

// Typical usage in Jest watch mode
const changedSourceFiles = new Set([
  "/project/src/utils.js",
  "/project/src/api/client.js"
]);

// Find all test files that need to re-run
const testFilter = (file: string) => 
  file.includes('__tests__') || 
  file.includes('.test.') || 
  file.includes('.spec.');

const affectedTests = dependencyResolver.resolveInverse(
  changedSourceFiles,
  testFilter
);

console.log("Tests to re-run:", affectedTests);
// Output: ["/project/src/__tests__/utils.test.js", "/project/src/api/__tests__/client.test.js"]

Analyzing Module Dependencies

// Get all direct dependencies of a module
const modulePath = "/project/src/components/Button.js";
const dependencies = dependencyResolver.resolve(modulePath);

console.log("Direct dependencies:", dependencies);
// Output: ["/project/src/styles.css", "/project/src/utils/helpers.js", "/project/node_modules/react/index.js"]

Building Dependency Maps

// Get detailed dependency information for impact analysis
const changedFiles = new Set(["/project/src/config.js"]);
const allFiles = (file: string) => true; // Include all file types

const dependencyMap = dependencyResolver.resolveInverseModuleMap(
  changedFiles,
  allFiles
);

dependencyMap.forEach(module => {
  console.log(`${module.file} depends on:`, module.dependencies);
});

Working with Snapshots

// The resolver automatically handles snapshot files
const snapshotFile = "/project/src/__tests__/__snapshots__/Button.test.js.snap";
const changedSnapshots = new Set([snapshotFile]);

// Find test files associated with changed snapshots
const relatedTests = dependencyResolver.resolveInverse(
  changedSnapshots,
  (file) => file.endsWith('.test.js')
);

console.log("Tests for changed snapshots:", relatedTests);
// Output: ["/project/src/__tests__/Button.test.js"]

Integration with Jest

This package is primarily used internally by Jest for:

  • Watch Mode: Determining which tests to re-run when files change
  • Dependency Tracking: Building dependency graphs for test environments
  • Mock Resolution: Handling Jest mock modules in dependency analysis
  • Snapshot Testing: Resolving relationships between snapshots and test files
  • Impact Analysis: Understanding how changes propagate through the codebase

The DependencyResolver integrates with Jest's core systems including the Haste file system, module resolver, and snapshot resolver to provide comprehensive dependency analysis capabilities.

For most users: You don't need to use this package directly. Jest automatically uses it internally for dependency resolution during test execution and watch mode. This documentation is primarily useful for Jest plugin developers or those building custom testing tools that need direct access to Jest's dependency resolution capabilities.