Socket
Socket
Sign inDemoInstall

jest-haste-map

Package Overview
Dependencies
37
Maintainers
2
Versions
270
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    jest-haste-map

`jest-haste-map` is a module used by Jest, a popular JavaScript testing framework, to create a fast lookup of files in a project. It helps Jest efficiently locate and track changes in files during testing, making it particularly useful for large projects


Version published
Weekly downloads
29M
decreased by-9.6%
Maintainers
2
Created
Weekly downloads
ย 

Package description

What is jest-haste-map?

The jest-haste-map npm package is a utility for building a Haste map, which is a mapping from module names to file paths. It is used by Jest to quickly resolve module dependencies for tests by keeping an in-memory map of all available modules. It can handle duplicate module names and provides a way to query the map for a specific module.

What are jest-haste-map's main functionalities?

Building a Haste Map

This code sample demonstrates how to create a new HasteMap instance with a configuration object, build the map, and then use it to get the module name for a specific file.

const HasteMap = require('jest-haste-map');
const config = {
  // These options are required:
  roots: ['/path/to/project'],
  extensions: ['js', 'json'],
  platforms: ['ios', 'android'],
  // You can provide additional options:
  computeSha1: true,
  // other options...
};
const hasteMap = new HasteMap(config);
hasteMap.build().then(map => {
  console.log(map.hasteFS.getModuleName('/path/to/project/file.js'));
});

Querying the Haste Map

This code sample shows how to query the built Haste map for the path of a module given its name, platform, and whether it supports the native platform.

hasteMap.build().then(map => {
  const modulePath = map.moduleMap.getModule('moduleName', 'platform', 'supportsNativePlatform');
  console.log(modulePath);
});

Other packages similar to jest-haste-map

Readme

Source

jest-haste-map

jest-haste-map is a module used by Jest, a popular JavaScript testing framework, to create a fast lookup of files in a project. It helps Jest efficiently locate and track changes in files during testing, making it particularly useful for large projects with many files.

why jest-haste-map ?

  • Parallel crawling and analysis: jest-haste-map crawls the entire project, extracts dependencies, and analyzes files in parallel across worker processes.This can significantly improve the performance of the map building process.
  • Cached file system: jest-haste-map keeps a cache of the file system in memory and on disk. This allows for fast file related operations, such as resolving module imports and checking for changes.
  • Minimal work: jest-haste-map only does the minimal amount of work necessary when files change. (If you are using watchman (recommended for large projects), Jest will ask watchman for changed files instead of crawling the file system. This is very fast even if you have tens of thousands of files.)
  • File system watching: jest-haste-map can watch the file system for changes. This is useful for building interactive tools, such as watch mode.

Installation

with npm :

npm install jest-haste-map --save-dev

with yarn :

yarn add jest-haste-map --dev

usage

jest-haste-map is compatible with both ES modules and CommonJS

simple usage

const map = new HasteMap.default({
  // options
});

Example usage (get all files with .js extension in the project)

import HasteMap from 'jest-haste-map';
import os from 'os';
import {dirname} from 'path';
import {fileURLToPath} from 'url';

const root = dirname(fileURLToPath(import.meta.url));

const map = new HasteMap.default({
  id: 'myproject', //Used for caching.
  extensions: ['js'], // Tells jest-haste-map to only crawl .js files.
  maxWorkers: os.availableParallelism(), //Parallelizes across all available CPUs.
  platforms: [], // This is only used for React Native, you can leave it empty.
  roots: [root], // Can be used to only search a subset of files within `rootDir`
  retainAllFiles: true,
  rootDir: root, //The project root.
});

const {hasteFS} = await map.build();

const files = hasteFS.getAllFiles();

console.log(files);

options

OptionTypeRequiredDefault Value
cacheDirectorystringNoos.tmpdir()
computeDependenciesbooleanNotrue
computeSha1booleanNofalse
consoleConsoleNo-
dependencyExtractorstring | nullNonull
enableSymlinksbooleanNofalse
extensionsArray<string>Yes-
forceNodeFilesystemAPIbooleanYes-
hasteImplModulePathstringYes-
hasteMapModulePathstringYes-
idstringYes-
ignorePatternHasteRegExpNo-
maxWorkersnumberYes-
mocksPatternstringNo-
platformsArray<string>Yes-
resetCachebooleanNo-
retainAllFilesbooleanYes-
rootDirstringYes-
rootsArray<string>Yes-
skipPackageJsonbooleanYes-
throwOnModuleCollisionbooleanYes-
useWatchmanbooleanNotrue

For more, you can check github

FAQs

Last updated on 16 Nov 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc