or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-jest-changed-files

A module used internally by Jest to check which files have changed since you last committed in git, hg, or sapling repositories.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/jest-changed-files@30.0.x

To install, run

npx @tessl/cli install tessl/npm-jest-changed-files@30.0.0

index.mddocs/

Jest Changed Files

Jest Changed Files is a utility module used internally by Jest to check which files have changed since the last commit in git, mercurial (hg), or sapling (sl) repositories. It provides file change detection functionality to enable Jest's ability to run tests only for changed code, significantly improving test performance in large codebases.

Package Information

  • Package Name: jest-changed-files
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install jest-changed-files

Core Imports

import { getChangedFilesForRoots, findRepos } from "jest-changed-files";
import type { ChangedFiles, ChangedFilesPromise } from "jest-changed-files";

For CommonJS:

const { getChangedFilesForRoots, findRepos } = require("jest-changed-files");

Basic Usage

import { getChangedFilesForRoots } from "jest-changed-files";

// Get changed files since last commit
const result = await getChangedFilesForRoots(["/path/to/project"], {
  lastCommit: true,
  withAncestor: false,
});

console.log(result.changedFiles); // Set of changed file paths
console.log(result.repos.git);    // Set of git repository paths

Capabilities

File Change Detection

Analyzes version control repositories to identify changed files based on various criteria. Supports git, mercurial (hg), and sapling (sl) repositories with configurable commit range detection.

/**
 * Get the list of files and repos that have changed since the last commit or specified reference
 * @param roots - Array of string paths (typically Jest roots configuration)
 * @param options - Configuration options for change detection
 * @returns Promise resolving to object with changedFiles and repos information
 */
function getChangedFilesForRoots(
  roots: Array<string>, 
  options: {
    /** Check only the last commit for changes */
    lastCommit?: boolean;
    /** Include ancestor commit in comparison (uses HEAD^ for git) */
    withAncestor?: boolean;
    /** Compare changes since specified commit, branch, or tag */
    changedSince?: string;
  }
): Promise<ChangedFiles>;

Usage Examples:

import { getChangedFilesForRoots } from "jest-changed-files";

// Check changes since last commit
const lastCommitChanges = await getChangedFilesForRoots(["/path/to/test"], {
  lastCommit: true,
  withAncestor: false,
});

// Check changes since specific branch/commit
const branchChanges = await getChangedFilesForRoots(["/path/to/test"], {
  changedSince: "main",
});

// Check changes with ancestor comparison
const ancestorChanges = await getChangedFilesForRoots(["/path/to/test"], {
  withAncestor: true,
});

Repository Discovery

Discovers version control repositories (git, mercurial, sapling) within specified root directories using concurrent scanning with resource limiting.

/**
 * Discover git, hg, and sapling repositories within specified root directories
 * @param roots - Array of directory paths to search for repositories
 * @returns Promise resolving to object containing Sets of repository paths by VCS type
 */
function findRepos(roots: Array<string>): Promise<{
  /** Set of git repository paths */
  git: Set<string>;
  /** Set of mercurial repository paths */
  hg: Set<string>;
  /** Set of sapling repository paths */
  sl: Set<string>;
}>;

Usage Example:

import { findRepos } from "jest-changed-files";

const repos = await findRepos(["/path/to/search", "/another/path"]);

console.log(repos.git); // Set<string> of git repository paths
console.log(repos.hg);  // Set<string> of mercurial repository paths  
console.log(repos.sl);  // Set<string> of sapling repository paths

Types

interface ChangedFiles {
  /** Repository information organized by VCS type */
  repos: {
    /** Set of git repository paths */
    git: Set<string>;
    /** Set of mercurial repository paths */
    hg: Set<string>;
    /** Set of sapling repository paths */
    sl: Set<string>;
  };
  /** Set of absolute file paths that have changed */
  changedFiles: Set<string>;
}

type ChangedFilesPromise = Promise<ChangedFiles>;

Implementation Notes

  • Concurrency Control: Uses p-limit with a maximum of 5 concurrent operations to prevent resource exhaustion when scanning many directories
  • Multiple VCS Support: Handles git, mercurial (hg), and sapling (sl) repositories with appropriate command-line interfaces
  • Path Resolution: All returned file paths are resolved to absolute paths for consistency
  • Resource Management: Implements mutex pattern to safely handle concurrent repository discovery across multiple root directories
  • Jest Integration: Designed specifically for Jest's changed file detection but can be used as a standalone utility