From 550d6c1a2256382b749bd0dba5daef197387341c Mon Sep 17 00:00:00 2001 From: cpojer Date: Mon, 17 Feb 2020 13:21:53 +0000 Subject: [PATCH] [jest-haste-map] Remove `mapper` option. --- CHANGELOG.md | 1 + .../src/__tests__/index.test.js | 1 - .../src/crawlers/__tests__/watchman.test.js | 51 ------------- .../jest-haste-map/src/crawlers/watchman.ts | 29 ++------ packages/jest-haste-map/src/index.ts | 73 +++++++++---------- packages/jest-haste-map/src/types.ts | 6 +- 6 files changed, 44 insertions(+), 117 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd86745e036..37ed3c5a8348 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[jest-config]` Support ESM config files with `.js` extension ([#9573](https://github.com/facebook/jest/9573)). - `[jest-runtime]` Override `module.createRequire` to return a Jest-compatible `require` function ([#9469](https://github.com/facebook/jest/pull/9469)) +- `[jest-haste-map]` [**BREAKING**] Remove `mapper` option ([#9581](https://github.com/facebook/jest/pull/9581)) - `[*]` Support array of paths for `moduleNameMapper` aliases ([#9465](https://github.com/facebook/jest/pull/9465)) - `[jest-reporters]` Adds ability to pass options to the istanbul-reporter through `coverageReporters` ([#9572](https://github.com/facebook/jest/pull/9572)) diff --git a/packages/jest-haste-map/src/__tests__/index.test.js b/packages/jest-haste-map/src/__tests__/index.test.js index 196094edd1c8..a24f9c3bddbf 100644 --- a/packages/jest-haste-map/src/__tests__/index.test.js +++ b/packages/jest-haste-map/src/__tests__/index.test.js @@ -426,7 +426,6 @@ describe('HasteMap', () => { const hasteMap = new HasteMap({ ...defaultConfig, computeSha1: true, - mapper: file => [file], maxWorkers: 1, useWatchman, }); diff --git a/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js b/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js index f72dc3fc5acb..b86340059106 100644 --- a/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js +++ b/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js @@ -44,7 +44,6 @@ const STRAWBERRY_RELATIVE = path.join(FRUITS_RELATIVE, 'strawberry.js'); const KIWI_RELATIVE = path.join(FRUITS_RELATIVE, 'kiwi.js'); const TOMATO_RELATIVE = path.join(FRUITS_RELATIVE, 'tomato.js'); const MELON_RELATIVE = path.join(VEGETABLES_RELATIVE, 'melon.json'); -const DURIAN_RELATIVE = path.join(VEGETABLES_RELATIVE, 'durian.zip'); const WATCH_PROJECT_MOCK = { [FRUITS]: { @@ -174,56 +173,6 @@ describe('watchman watch', () => { expect(client.end).toBeCalled(); })); - test('applies the mapper when needed', () => { - mockResponse = { - 'list-capabilities': { - [undefined]: { - capabilities: ['field-content.sha1hex'], - }, - }, - query: { - [ROOT_MOCK]: { - clock: 'c:fake-clock:1', - files: [ - { - exists: true, - mtime_ms: {toNumber: () => 33}, - name: 'vegetables/durian.zip', - size: 43, - }, - ], - is_fresh_instance: true, - version: '4.5.0', - }, - }, - 'watch-project': WATCH_PROJECT_MOCK, - }; - - return watchmanCrawl({ - data: { - clocks: new Map(), - files: new Map(), - }, - extensions: ['js', 'json', 'zip'], - ignore: pearMatcher, - mapper: n => - n.endsWith('.zip') - ? [path.join(n, 'foo.1.js'), path.join(n, 'foo.2.js')] - : null, - rootDir: ROOT_MOCK, - roots: ROOTS, - }).then(({changedFiles, hasteMap, removedFiles}) => { - expect(changedFiles).toEqual(undefined); - expect(hasteMap.files).toEqual( - createMap({ - [path.join(DURIAN_RELATIVE, 'foo.1.js')]: ['', 33, 43, 0, '', null], - [path.join(DURIAN_RELATIVE, 'foo.2.js')]: ['', 33, 43, 0, '', null], - }), - ); - expect(removedFiles).toEqual(new Map()); - }); - }); - test('updates file map and removedFiles when the clock is given', () => { mockResponse = { 'list-capabilities': { diff --git a/packages/jest-haste-map/src/crawlers/watchman.ts b/packages/jest-haste-map/src/crawlers/watchman.ts index a3b4982d6c39..a9a607226420 100644 --- a/packages/jest-haste-map/src/crawlers/watchman.ts +++ b/packages/jest-haste-map/src/crawlers/watchman.ts @@ -7,7 +7,7 @@ import * as path from 'path'; import watchman = require('fb-watchman'); -import {Config} from '@jest/types'; +import { Config } from '@jest/types'; import * as fastPath from '../lib/fast_path'; import normalizePathSep from '../lib/normalizePathSep'; import H from '../constants'; @@ -38,7 +38,7 @@ export = async function watchmanCrawl( hasteMap: InternalHasteMap; }> { const fields = ['name', 'exists', 'mtime_ms', 'size']; - const {data, extensions, ignore, rootDir, roots} = options; + const { data, extensions, ignore, rootDir, roots } = options; const defaultWatchExpression = [ 'allof', ['type', 'f'], @@ -59,7 +59,7 @@ export = async function watchmanCrawl( ); if (options.computeSha1) { - const {capabilities} = await cmd('list-capabilities'); + const { capabilities } = await cmd('list-capabilities'); if (capabilities.indexOf('field-content.sha1hex') !== -1) { fields.push('content.sha1hex'); @@ -125,9 +125,9 @@ export = async function watchmanCrawl( const relativeRoot = fastPath.relative(rootDir, root); const query = clocks.has(relativeRoot) ? // Use the `since` generator if we have a clock available - {expression, fields, since: clocks.get(relativeRoot)} + { expression, fields, since: clocks.get(relativeRoot) } : // Otherwise use the `glob` filter - {expression, fields, glob}; + { expression, fields, glob }; const response = await cmd('query', root, query); @@ -235,23 +235,8 @@ export = async function watchmanCrawl( nextData = ['', mtime, size, 0, '', sha1hex]; } - const mappings = options.mapper ? options.mapper(filePath) : null; - - if (mappings) { - for (const absoluteVirtualFilePath of mappings) { - if (!ignore(absoluteVirtualFilePath)) { - const relativeVirtualFilePath = fastPath.relative( - rootDir, - absoluteVirtualFilePath, - ); - files.set(relativeVirtualFilePath, nextData); - changedFiles.set(relativeVirtualFilePath, nextData); - } - } - } else { - files.set(relativeFilePath, nextData); - changedFiles.set(relativeFilePath, nextData); - } + files.set(relativeFilePath, nextData); + changedFiles.set(relativeFilePath, nextData); } } } diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index 2e5e827a59ba..13c2fd070992 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -5,17 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -import {execSync} from 'child_process'; -import {createHash} from 'crypto'; -import {EventEmitter} from 'events'; +import { execSync } from 'child_process'; +import { createHash } from 'crypto'; +import { EventEmitter } from 'events'; import * as fs from 'fs'; -import {tmpdir} from 'os'; +import { tmpdir } from 'os'; import * as path from 'path'; -import {NodeWatcher, Watcher as SaneWatcher} from 'sane'; -import {Config} from '@jest/types'; +import { NodeWatcher, Watcher as SaneWatcher } from 'sane'; +import { Config } from '@jest/types'; import serializer from 'jest-serializer'; import Worker from 'jest-worker'; -import {getSha1, worker} from './worker'; +import { getSha1, worker } from './worker'; import getMockName from './getMockName'; import getPlatformExtension from './lib/getPlatformExtension'; import H from './constants'; @@ -39,7 +39,6 @@ import { HasteRegExp, InternalHasteMap, HasteMap as InternalHasteMapObject, - Mapper, MockData, ModuleMapData, ModuleMetaData, @@ -58,7 +57,6 @@ type Options = { forceNodeFilesystemAPI?: boolean; hasteImplModulePath?: string; ignorePattern?: HasteRegExp; - mapper?: Mapper; maxWorkers: number; mocksPattern?: string; name: string; @@ -83,7 +81,6 @@ type InternalOptions = { forceNodeFilesystemAPI: boolean; hasteImplModulePath?: string; ignorePattern?: HasteRegExp; - mapper?: Mapper; maxWorkers: number; mocksPattern: RegExp | null; name: string; @@ -102,7 +99,7 @@ type Watcher = { close(callback: () => void): void; }; -type WorkerInterface = {worker: typeof worker; getSha1: typeof getSha1}; +type WorkerInterface = { worker: typeof worker; getSha1: typeof getSha1 }; // TODO: Ditch namespace when this module exports ESM namespace HasteMap { @@ -120,13 +117,13 @@ const PACKAGE_JSON = path.sep + 'package.json'; // TypeScript doesn't like us importing from outside `rootDir`, but it doesn't // understand `require`. -const {version: VERSION} = require('../package.json'); +const { version: VERSION } = require('../package.json'); const canUseWatchman = ((): boolean => { try { - execSync('watchman --version', {stdio: ['ignore']}); + execSync('watchman --version', { stdio: ['ignore'] }); return true; - } catch (e) {} + } catch (e) { } return false; })(); @@ -140,12 +137,12 @@ const getWhiteList = (list: Array | undefined): RegExp | null => { ); return new RegExp( '(' + - escapePathSeparator(NODE_MODULES) + - '(?:' + - newList.join('|') + - ')(?=$|' + - escapePathSeparator(path.sep) + - '))', + escapePathSeparator(NODE_MODULES) + + '(?:' + + newList.join('|') + + ')(?=$|' + + escapePathSeparator(path.sep) + + '))', 'g', ); } @@ -261,7 +258,6 @@ class HasteMap extends EventEmitter { forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI, hasteImplModulePath: options.hasteImplModulePath, ignorePattern: options.ignorePattern, - mapper: options.mapper, maxWorkers: options.maxWorkers, mocksPattern: options.mocksPattern ? new RegExp(options.mocksPattern) @@ -281,7 +277,7 @@ class HasteMap extends EventEmitter { if (options.ignorePattern && !(options.ignorePattern instanceof RegExp)) { this._console.warn( 'jest-haste-map: the `ignorePattern` options as a function is being ' + - 'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.', + 'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.', ); } @@ -437,7 +433,7 @@ class HasteMap extends EventEmitter { map: ModuleMapData, mocks: MockData, filePath: Config.Path, - workerOptions?: {forceInBand: boolean}, + workerOptions?: { forceInBand: boolean }, ): Promise | null { const rootDir = this._options.rootDir; @@ -651,7 +647,7 @@ class HasteMap extends EventEmitter { changedFiles?: FileData; hasteMap: InternalHasteMap; }): Promise { - const {removedFiles, changedFiles, hasteMap} = data; + const { removedFiles, changedFiles, hasteMap } = data; // If any files were removed or we did not track what files changed, process // every file looking for changes. Otherwise, process only changed files. @@ -727,10 +723,10 @@ class HasteMap extends EventEmitter { /** * Creates workers or parses files and extracts metadata in-process. */ - private _getWorker(options?: {forceInBand: boolean}): WorkerInterface { + private _getWorker(options?: { forceInBand: boolean }): WorkerInterface { if (!this._worker) { if ((options && options.forceInBand) || this._options.maxWorkers <= 1) { - this._worker = {getSha1, worker}; + this._worker = { getSha1, worker }; } else { // @ts-ignore: assignment of a worker with custom properties. this._worker = new Worker(require.resolve('./worker'), { @@ -755,7 +751,6 @@ class HasteMap extends EventEmitter { extensions: options.extensions, forceNodeFilesystemAPI: options.forceNodeFilesystemAPI, ignore, - mapper: options.mapper, rootDir: options.rootDir, roots: options.roots, }; @@ -764,18 +759,18 @@ class HasteMap extends EventEmitter { if (crawl === watchmanCrawl) { this._console.warn( `jest-haste-map: Watchman crawl failed. Retrying once with node ` + - `crawler.\n` + - ` Usually this happens when watchman isn't running. Create an ` + - `empty \`.watchmanconfig\` file in your project's root folder or ` + - `initialize a git or hg repository in your project.\n` + - ` ` + - error, + `crawler.\n` + + ` Usually this happens when watchman isn't running. Create an ` + + `empty \`.watchmanconfig\` file in your project's root folder or ` + + `initialize a git or hg repository in your project.\n` + + ` ` + + error, ); return nodeCrawl(crawlerOptions).catch(e => { throw new Error( `Crawler retry failed:\n` + - ` Original error: ${error.message}\n` + - ` Retry error: ${e.message}\n`, + ` Original error: ${error.message}\n` + + ` Retry error: ${e.message}\n`, ); }); } @@ -808,8 +803,8 @@ class HasteMap extends EventEmitter { canUseWatchman && this._options.useWatchman ? WatchmanWatcher : FSEventsWatcher.isSupported() - ? FSEventsWatcher - : NodeWatcher; + ? FSEventsWatcher + : NodeWatcher; const extensions = this._options.extensions; const ignorePattern = this._options.ignorePattern; @@ -907,7 +902,7 @@ class HasteMap extends EventEmitter { } const add = () => { - eventsQueue.push({filePath, stat, type}); + eventsQueue.push({ filePath, stat, type }); return null; }; @@ -967,7 +962,7 @@ class HasteMap extends EventEmitter { hasteMap.map, hasteMap.mocks, filePath, - {forceInBand: true}, + { forceInBand: true }, ); // Cleanup this._cleanup(); diff --git a/packages/jest-haste-map/src/types.ts b/packages/jest-haste-map/src/types.ts index aa1b4ee5e24e..d2ed52bbdd0d 100644 --- a/packages/jest-haste-map/src/types.ts +++ b/packages/jest-haste-map/src/types.ts @@ -6,12 +6,11 @@ */ import * as fs from 'fs'; -import {Config} from '@jest/types'; +import { Config } from '@jest/types'; import ModuleMap from './ModuleMap'; import HasteFS from './HasteFS'; export type IgnoreMatcher = (item: string) => boolean; -export type Mapper = (item: string) => Array | null; export type WorkerMessage = { computeDependencies: boolean; @@ -35,7 +34,6 @@ export type CrawlerOptions = { extensions: Array; forceNodeFilesystemAPI: boolean; ignore: IgnoreMatcher; - mapper?: Mapper | null; rootDir: string; roots: Array; }; @@ -84,7 +82,7 @@ export type RawModuleMap = { mocks: MockData; }; -type ModuleMapItem = {[platform: string]: ModuleMetaData}; +type ModuleMapItem = { [platform: string]: ModuleMetaData }; export type ModuleMetaData = [Config.Path, /* type */ number]; export type HType = {