Dependency resolution utilities for Jest testing framework
npx @tessl/cli install tessl/npm-jest-resolve-dependencies@30.1.0Jest 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.
npm install jest-resolve-dependenciesThis package requires additional Jest packages for full functionality:
npm install jest-haste-map jest-resolve jest-snapshotNote: 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.
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");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-runJest Resolve Dependencies is built around key dependency resolution concepts:
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>;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>;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>;/**
* 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;
}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"]// 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"]// 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);
});// 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"]This package is primarily used internally by Jest for:
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.