|
| 1 | +# jest-haste-map |
| 2 | + |
| 3 | +`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. |
| 4 | + |
| 5 | +## why jest-haste-map ? |
| 6 | + |
| 7 | +- **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. |
| 8 | +- **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. |
| 9 | +- **Minimal work**: jest-haste-map only does the minimal amount of work necessary when files change. (If you are using [watchman](https://facebook.github.io/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.) |
| 10 | +- **File system watching:** jest-haste-map can watch the file system for changes. This is useful for building interactive tools, such as watch mode. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +with npm : |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install jest-haste-map --save-dev |
| 18 | +``` |
| 19 | + |
| 20 | +with yarn : |
| 21 | + |
| 22 | +```bash |
| 23 | +yarn add jest-haste-map --dev |
| 24 | +``` |
| 25 | + |
| 26 | +## usage |
| 27 | + |
| 28 | +`jest-haste-map` is compatible with both `ES modules` and `CommonJS` |
| 29 | + |
| 30 | +### simple usage |
| 31 | + |
| 32 | +```javascript |
| 33 | +const map = new HasteMap.default({ |
| 34 | + // options |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +### Example usage (get all files with .js extension in the project) |
| 39 | + |
| 40 | +```javascript |
| 41 | +import HasteMap from 'jest-haste-map'; |
| 42 | +import os from 'os'; |
| 43 | +import {dirname} from 'path'; |
| 44 | +import {fileURLToPath} from 'url'; |
| 45 | + |
| 46 | +const root = dirname(fileURLToPath(import.meta.url)); |
| 47 | + |
| 48 | +const map = new HasteMap.default({ |
| 49 | + id: 'myproject', //Used for caching. |
| 50 | + extensions: ['js'], // Tells jest-haste-map to only crawl .js files. |
| 51 | + maxWorkers: os.availableParallelism(), //Parallelizes across all available CPUs. |
| 52 | + platforms: [], // This is only used for React Native, you can leave it empty. |
| 53 | + roots: [root], // Can be used to only search a subset of files within `rootDir` |
| 54 | + retainAllFiles: true, |
| 55 | + rootDir: root, //The project root. |
| 56 | +}); |
| 57 | + |
| 58 | +const {hasteFS} = await map.build(); |
| 59 | + |
| 60 | +const files = hasteFS.getAllFiles(); |
| 61 | + |
| 62 | +console.log(files); |
| 63 | +``` |
| 64 | +
|
| 65 | +### options |
| 66 | +
|
| 67 | +| Option | Type | Required | Default Value | |
| 68 | +| ---------------------- | ------------------- | -------- | ------------- | |
| 69 | +| cacheDirectory | string | No | `os.tmpdir()` | |
| 70 | +| computeDependencies | boolean | No | `true` | |
| 71 | +| computeSha1 | boolean | No | `false` | |
| 72 | +| console | Console | No | - | |
| 73 | +| dependencyExtractor | string \| null | No | `null` | |
| 74 | +| enableSymlinks | boolean | No | `false` | |
| 75 | +| extensions | Array<string> | Yes | - | |
| 76 | +| forceNodeFilesystemAPI | boolean | Yes | - | |
| 77 | +| hasteImplModulePath | string | Yes | - | |
| 78 | +| hasteMapModulePath | string | Yes | - | |
| 79 | +| id | string | Yes | - | |
| 80 | +| ignorePattern | HasteRegExp | No | - | |
| 81 | +| maxWorkers | number | Yes | - | |
| 82 | +| mocksPattern | string | No | - | |
| 83 | +| platforms | Array<string> | Yes | - | |
| 84 | +| resetCache | boolean | No | - | |
| 85 | +| retainAllFiles | boolean | Yes | - | |
| 86 | +| rootDir | string | Yes | - | |
| 87 | +| roots | Array<string> | Yes | - | |
| 88 | +| skipPackageJson | boolean | Yes | - | |
| 89 | +| throwOnModuleCollision | boolean | Yes | - | |
| 90 | +| useWatchman | boolean | No | `true` | |
| 91 | +
|
| 92 | +For more, you can check [github](https://github.com/jestjs/jest/tree/main/packages/jest-haste-map) |
0 commit comments