CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jest-haste-map

A module used by Jest to create a fast lookup of files in a project

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

jest-haste-map

jest-haste-map is a high-performance file system mapping and watching library designed for efficient project file discovery and change tracking. It serves as a core component of the Jest testing framework, providing fast file lookups, parallel crawling capabilities, and cached file system operations that significantly improve performance in large JavaScript/TypeScript projects.

Package Information

  • Package Name: jest-haste-map
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install jest-haste-map

Core Imports

import HasteMap, { ModuleMap } from "jest-haste-map";

For CommonJS:

const HasteMap = require("jest-haste-map").default;
const { ModuleMap } = require("jest-haste-map");

Basic Usage

import HasteMap from "jest-haste-map";
import os from "os";

// Create a haste map for your project
const hasteMap = await HasteMap.create({
  id: "myproject",
  extensions: ["js", "ts", "jsx", "tsx"],
  maxWorkers: os.availableParallelism(),
  platforms: [], // For React Native support
  roots: ["/path/to/project"],
  retainAllFiles: true,
  rootDir: "/path/to/project",
});

// Build the file system map
const { hasteFS, moduleMap } = await hasteMap.build();

// Use the virtual file system
const allFiles = hasteFS.getAllFiles();
const dependencies = hasteFS.getDependencies("/path/to/file.js");

// Resolve modules
const modulePath = moduleMap.getModule("MyComponent");

Architecture

jest-haste-map is built around several key components:

  • HasteMap: Main orchestrator that manages file system crawling, caching, and watching
  • HasteFS: Virtual file system interface providing fast file operations and metadata access
  • ModuleMap: Module resolution system supporting platform-specific files and package.json resolution
  • Parallel Crawling: Worker processes for concurrent file analysis and dependency extraction
  • Caching System: In-memory and disk-based caches for optimal performance
  • File Watching: Real-time file system monitoring using Watchman, FSEvents, or Node.js fs.watch

Capabilities

File System Mapping

Core file system crawling and mapping functionality for creating fast lookups of project files. Supports parallel processing and intelligent caching.

class HasteMap extends EventEmitter implements IHasteMap {
  static create(options: Options): Promise<IHasteMap>;
  build(): Promise<{hasteFS: IHasteFS; moduleMap: IModuleMap}>;
  end(): Promise<void>;
}

interface Options {
  id: string;
  extensions: Array<string>;
  maxWorkers: number;
  platforms: Array<string>;
  retainAllFiles: boolean;
  rootDir: string;
  roots: Array<string>;
  cacheDirectory?: string;
  computeDependencies?: boolean;
  computeSha1?: boolean;
  console?: Console;
  dependencyExtractor?: string | null;
  enableSymlinks?: boolean;
  forceNodeFilesystemAPI?: boolean;
  hasteImplModulePath?: string;
  hasteMapModulePath?: string;
  ignorePattern?: HasteRegExp;
  mocksPattern?: string;
  resetCache?: boolean;
  skipPackageJson?: boolean;
  throwOnModuleCollision?: boolean;
  useWatchman?: boolean;
  watch?: boolean;
  workerThreads?: boolean;
}

File System Mapping

Virtual File System

Fast file system operations through a virtual interface that provides cached access to file metadata, dependencies, and content information.

interface IHasteFS {
  exists(path: string): boolean;
  getAllFiles(): Array<string>;
  getAbsoluteFileIterator(): Iterable<string>;
  getDependencies(file: string): Array<string> | null;
  getSize(path: string): number | null;
  getSha1(file: string): string | null;
  matchFiles(pattern: RegExp | string): Array<string>;
  matchFilesWithGlob(globs: ReadonlyArray<string>, root: string | null): Set<string>;
  getModuleName(file: string): string | null;
}

Virtual File System

Module Resolution

Module resolution system that maps module names to file paths with support for platform-specific extensions, package.json resolution, and mock files.

interface IModuleMap {
  getModule(
    name: string,
    platform?: string | null,
    supportsNativePlatform?: boolean | null,
    type?: HTypeValue | null
  ): string | null;
  getPackage(
    name: string,
    platform: string | null | undefined,
    _supportsNativePlatform: boolean | null
  ): string | null;
  getMockModule(name: string): string | undefined;
  getRawModuleMap(): RawModuleMap;
  toJSON(): SerializableModuleMap;
}

const ModuleMap = {
  create: (rootPath: string) => IModuleMap;
};

Module Resolution

File System Watching

Real-time file system monitoring for interactive development tools, providing change events and automatic map updates.

interface IHasteMap {
  on(eventType: 'change', handler: (event: ChangeEvent) => void): void;
}

interface ChangeEvent {
  eventsQueue: EventsQueue;
  hasteFS: IHasteFS;
  moduleMap: IModuleMap;
}

File System Watching

Core Types

type HasteRegExp = RegExp | ((str: string) => boolean);

interface HasteMapStatic {
  getCacheFilePath(tmpdir: string, name: string, ...extra: Array<string>): string;
  getModuleMapFromJSON(json: SerializableModuleMap): IModuleMap;
}

interface EventsQueue extends Array<{
  filePath: string;
  stat: Stats | undefined;
  type: string;
}> {}

interface SerializableModuleMap {
  duplicates: ReadonlyArray<[string, [string, [string, [string, number]]]]>;
  map: ReadonlyArray<[string, ModuleMapItem]>;
  mocks: ReadonlyArray<[string, string]>;
  rootDir: string;
}

type ModuleMapItem = {[platform: string]: ModuleMetaData};
type ModuleMetaData = [path: string, type: number];

type DuplicatesSet = Map<string, number>;
type DuplicatesIndex = Map<string, Map<string, DuplicatesSet>>;

class DuplicateHasteCandidatesError extends Error {
  hasteName: string;
  platform: string | null;
  supportsNativePlatform: boolean;
  duplicatesSet: DuplicatesSet;
  constructor(
    name: string,
    platform: string,
    supportsNativePlatform: boolean,
    duplicatesSet: DuplicatesSet
  );
}

docs

file-system-mapping.md

file-system-watching.md

index.md

module-resolution.md

virtual-file-system.md

tile.json