Skip to content

Commit 550d6c1

Browse files
committed
[jest-haste-map] Remove mapper option.
1 parent 63593a2 commit 550d6c1

File tree

6 files changed

+44
-117
lines changed

6 files changed

+44
-117
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- `[jest-config]` Support ESM config files with `.js` extension ([#9573](https://github.com/facebook/jest/9573)).
66
- `[jest-runtime]` Override `module.createRequire` to return a Jest-compatible `require` function ([#9469](https://github.com/facebook/jest/pull/9469))
7+
- `[jest-haste-map]` [**BREAKING**] Remove `mapper` option ([#9581](https://github.com/facebook/jest/pull/9581))
78
- `[*]` Support array of paths for `moduleNameMapper` aliases ([#9465](https://github.com/facebook/jest/pull/9465))
89
- `[jest-reporters]` Adds ability to pass options to the istanbul-reporter through `coverageReporters` ([#9572](https://github.com/facebook/jest/pull/9572))
910

packages/jest-haste-map/src/__tests__/index.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ describe('HasteMap', () => {
426426
const hasteMap = new HasteMap({
427427
...defaultConfig,
428428
computeSha1: true,
429-
mapper: file => [file],
430429
maxWorkers: 1,
431430
useWatchman,
432431
});

packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ const STRAWBERRY_RELATIVE = path.join(FRUITS_RELATIVE, 'strawberry.js');
4444
const KIWI_RELATIVE = path.join(FRUITS_RELATIVE, 'kiwi.js');
4545
const TOMATO_RELATIVE = path.join(FRUITS_RELATIVE, 'tomato.js');
4646
const MELON_RELATIVE = path.join(VEGETABLES_RELATIVE, 'melon.json');
47-
const DURIAN_RELATIVE = path.join(VEGETABLES_RELATIVE, 'durian.zip');
4847

4948
const WATCH_PROJECT_MOCK = {
5049
[FRUITS]: {
@@ -174,56 +173,6 @@ describe('watchman watch', () => {
174173
expect(client.end).toBeCalled();
175174
}));
176175

177-
test('applies the mapper when needed', () => {
178-
mockResponse = {
179-
'list-capabilities': {
180-
[undefined]: {
181-
capabilities: ['field-content.sha1hex'],
182-
},
183-
},
184-
query: {
185-
[ROOT_MOCK]: {
186-
clock: 'c:fake-clock:1',
187-
files: [
188-
{
189-
exists: true,
190-
mtime_ms: {toNumber: () => 33},
191-
name: 'vegetables/durian.zip',
192-
size: 43,
193-
},
194-
],
195-
is_fresh_instance: true,
196-
version: '4.5.0',
197-
},
198-
},
199-
'watch-project': WATCH_PROJECT_MOCK,
200-
};
201-
202-
return watchmanCrawl({
203-
data: {
204-
clocks: new Map(),
205-
files: new Map(),
206-
},
207-
extensions: ['js', 'json', 'zip'],
208-
ignore: pearMatcher,
209-
mapper: n =>
210-
n.endsWith('.zip')
211-
? [path.join(n, 'foo.1.js'), path.join(n, 'foo.2.js')]
212-
: null,
213-
rootDir: ROOT_MOCK,
214-
roots: ROOTS,
215-
}).then(({changedFiles, hasteMap, removedFiles}) => {
216-
expect(changedFiles).toEqual(undefined);
217-
expect(hasteMap.files).toEqual(
218-
createMap({
219-
[path.join(DURIAN_RELATIVE, 'foo.1.js')]: ['', 33, 43, 0, '', null],
220-
[path.join(DURIAN_RELATIVE, 'foo.2.js')]: ['', 33, 43, 0, '', null],
221-
}),
222-
);
223-
expect(removedFiles).toEqual(new Map());
224-
});
225-
});
226-
227176
test('updates file map and removedFiles when the clock is given', () => {
228177
mockResponse = {
229178
'list-capabilities': {

packages/jest-haste-map/src/crawlers/watchman.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import * as path from 'path';
99
import watchman = require('fb-watchman');
10-
import {Config} from '@jest/types';
10+
import { Config } from '@jest/types';
1111
import * as fastPath from '../lib/fast_path';
1212
import normalizePathSep from '../lib/normalizePathSep';
1313
import H from '../constants';
@@ -38,7 +38,7 @@ export = async function watchmanCrawl(
3838
hasteMap: InternalHasteMap;
3939
}> {
4040
const fields = ['name', 'exists', 'mtime_ms', 'size'];
41-
const {data, extensions, ignore, rootDir, roots} = options;
41+
const { data, extensions, ignore, rootDir, roots } = options;
4242
const defaultWatchExpression = [
4343
'allof',
4444
['type', 'f'],
@@ -59,7 +59,7 @@ export = async function watchmanCrawl(
5959
);
6060

6161
if (options.computeSha1) {
62-
const {capabilities} = await cmd('list-capabilities');
62+
const { capabilities } = await cmd('list-capabilities');
6363

6464
if (capabilities.indexOf('field-content.sha1hex') !== -1) {
6565
fields.push('content.sha1hex');
@@ -125,9 +125,9 @@ export = async function watchmanCrawl(
125125
const relativeRoot = fastPath.relative(rootDir, root);
126126
const query = clocks.has(relativeRoot)
127127
? // Use the `since` generator if we have a clock available
128-
{expression, fields, since: clocks.get(relativeRoot)}
128+
{ expression, fields, since: clocks.get(relativeRoot) }
129129
: // Otherwise use the `glob` filter
130-
{expression, fields, glob};
130+
{ expression, fields, glob };
131131

132132
const response = await cmd('query', root, query);
133133

@@ -235,23 +235,8 @@ export = async function watchmanCrawl(
235235
nextData = ['', mtime, size, 0, '', sha1hex];
236236
}
237237

238-
const mappings = options.mapper ? options.mapper(filePath) : null;
239-
240-
if (mappings) {
241-
for (const absoluteVirtualFilePath of mappings) {
242-
if (!ignore(absoluteVirtualFilePath)) {
243-
const relativeVirtualFilePath = fastPath.relative(
244-
rootDir,
245-
absoluteVirtualFilePath,
246-
);
247-
files.set(relativeVirtualFilePath, nextData);
248-
changedFiles.set(relativeVirtualFilePath, nextData);
249-
}
250-
}
251-
} else {
252-
files.set(relativeFilePath, nextData);
253-
changedFiles.set(relativeFilePath, nextData);
254-
}
238+
files.set(relativeFilePath, nextData);
239+
changedFiles.set(relativeFilePath, nextData);
255240
}
256241
}
257242
}

packages/jest-haste-map/src/index.ts

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {execSync} from 'child_process';
9-
import {createHash} from 'crypto';
10-
import {EventEmitter} from 'events';
8+
import { execSync } from 'child_process';
9+
import { createHash } from 'crypto';
10+
import { EventEmitter } from 'events';
1111
import * as fs from 'fs';
12-
import {tmpdir} from 'os';
12+
import { tmpdir } from 'os';
1313
import * as path from 'path';
14-
import {NodeWatcher, Watcher as SaneWatcher} from 'sane';
15-
import {Config} from '@jest/types';
14+
import { NodeWatcher, Watcher as SaneWatcher } from 'sane';
15+
import { Config } from '@jest/types';
1616
import serializer from 'jest-serializer';
1717
import Worker from 'jest-worker';
18-
import {getSha1, worker} from './worker';
18+
import { getSha1, worker } from './worker';
1919
import getMockName from './getMockName';
2020
import getPlatformExtension from './lib/getPlatformExtension';
2121
import H from './constants';
@@ -39,7 +39,6 @@ import {
3939
HasteRegExp,
4040
InternalHasteMap,
4141
HasteMap as InternalHasteMapObject,
42-
Mapper,
4342
MockData,
4443
ModuleMapData,
4544
ModuleMetaData,
@@ -58,7 +57,6 @@ type Options = {
5857
forceNodeFilesystemAPI?: boolean;
5958
hasteImplModulePath?: string;
6059
ignorePattern?: HasteRegExp;
61-
mapper?: Mapper;
6260
maxWorkers: number;
6361
mocksPattern?: string;
6462
name: string;
@@ -83,7 +81,6 @@ type InternalOptions = {
8381
forceNodeFilesystemAPI: boolean;
8482
hasteImplModulePath?: string;
8583
ignorePattern?: HasteRegExp;
86-
mapper?: Mapper;
8784
maxWorkers: number;
8885
mocksPattern: RegExp | null;
8986
name: string;
@@ -102,7 +99,7 @@ type Watcher = {
10299
close(callback: () => void): void;
103100
};
104101

105-
type WorkerInterface = {worker: typeof worker; getSha1: typeof getSha1};
102+
type WorkerInterface = { worker: typeof worker; getSha1: typeof getSha1 };
106103

107104
// TODO: Ditch namespace when this module exports ESM
108105
namespace HasteMap {
@@ -120,13 +117,13 @@ const PACKAGE_JSON = path.sep + 'package.json';
120117

121118
// TypeScript doesn't like us importing from outside `rootDir`, but it doesn't
122119
// understand `require`.
123-
const {version: VERSION} = require('../package.json');
120+
const { version: VERSION } = require('../package.json');
124121

125122
const canUseWatchman = ((): boolean => {
126123
try {
127-
execSync('watchman --version', {stdio: ['ignore']});
124+
execSync('watchman --version', { stdio: ['ignore'] });
128125
return true;
129-
} catch (e) {}
126+
} catch (e) { }
130127
return false;
131128
})();
132129

@@ -140,12 +137,12 @@ const getWhiteList = (list: Array<string> | undefined): RegExp | null => {
140137
);
141138
return new RegExp(
142139
'(' +
143-
escapePathSeparator(NODE_MODULES) +
144-
'(?:' +
145-
newList.join('|') +
146-
')(?=$|' +
147-
escapePathSeparator(path.sep) +
148-
'))',
140+
escapePathSeparator(NODE_MODULES) +
141+
'(?:' +
142+
newList.join('|') +
143+
')(?=$|' +
144+
escapePathSeparator(path.sep) +
145+
'))',
149146
'g',
150147
);
151148
}
@@ -261,7 +258,6 @@ class HasteMap extends EventEmitter {
261258
forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI,
262259
hasteImplModulePath: options.hasteImplModulePath,
263260
ignorePattern: options.ignorePattern,
264-
mapper: options.mapper,
265261
maxWorkers: options.maxWorkers,
266262
mocksPattern: options.mocksPattern
267263
? new RegExp(options.mocksPattern)
@@ -281,7 +277,7 @@ class HasteMap extends EventEmitter {
281277
if (options.ignorePattern && !(options.ignorePattern instanceof RegExp)) {
282278
this._console.warn(
283279
'jest-haste-map: the `ignorePattern` options as a function is being ' +
284-
'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.',
280+
'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.',
285281
);
286282
}
287283

@@ -437,7 +433,7 @@ class HasteMap extends EventEmitter {
437433
map: ModuleMapData,
438434
mocks: MockData,
439435
filePath: Config.Path,
440-
workerOptions?: {forceInBand: boolean},
436+
workerOptions?: { forceInBand: boolean },
441437
): Promise<void> | null {
442438
const rootDir = this._options.rootDir;
443439

@@ -651,7 +647,7 @@ class HasteMap extends EventEmitter {
651647
changedFiles?: FileData;
652648
hasteMap: InternalHasteMap;
653649
}): Promise<InternalHasteMap> {
654-
const {removedFiles, changedFiles, hasteMap} = data;
650+
const { removedFiles, changedFiles, hasteMap } = data;
655651

656652
// If any files were removed or we did not track what files changed, process
657653
// every file looking for changes. Otherwise, process only changed files.
@@ -727,10 +723,10 @@ class HasteMap extends EventEmitter {
727723
/**
728724
* Creates workers or parses files and extracts metadata in-process.
729725
*/
730-
private _getWorker(options?: {forceInBand: boolean}): WorkerInterface {
726+
private _getWorker(options?: { forceInBand: boolean }): WorkerInterface {
731727
if (!this._worker) {
732728
if ((options && options.forceInBand) || this._options.maxWorkers <= 1) {
733-
this._worker = {getSha1, worker};
729+
this._worker = { getSha1, worker };
734730
} else {
735731
// @ts-ignore: assignment of a worker with custom properties.
736732
this._worker = new Worker(require.resolve('./worker'), {
@@ -755,7 +751,6 @@ class HasteMap extends EventEmitter {
755751
extensions: options.extensions,
756752
forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
757753
ignore,
758-
mapper: options.mapper,
759754
rootDir: options.rootDir,
760755
roots: options.roots,
761756
};
@@ -764,18 +759,18 @@ class HasteMap extends EventEmitter {
764759
if (crawl === watchmanCrawl) {
765760
this._console.warn(
766761
`jest-haste-map: Watchman crawl failed. Retrying once with node ` +
767-
`crawler.\n` +
768-
` Usually this happens when watchman isn't running. Create an ` +
769-
`empty \`.watchmanconfig\` file in your project's root folder or ` +
770-
`initialize a git or hg repository in your project.\n` +
771-
` ` +
772-
error,
762+
`crawler.\n` +
763+
` Usually this happens when watchman isn't running. Create an ` +
764+
`empty \`.watchmanconfig\` file in your project's root folder or ` +
765+
`initialize a git or hg repository in your project.\n` +
766+
` ` +
767+
error,
773768
);
774769
return nodeCrawl(crawlerOptions).catch(e => {
775770
throw new Error(
776771
`Crawler retry failed:\n` +
777-
` Original error: ${error.message}\n` +
778-
` Retry error: ${e.message}\n`,
772+
` Original error: ${error.message}\n` +
773+
` Retry error: ${e.message}\n`,
779774
);
780775
});
781776
}
@@ -808,8 +803,8 @@ class HasteMap extends EventEmitter {
808803
canUseWatchman && this._options.useWatchman
809804
? WatchmanWatcher
810805
: FSEventsWatcher.isSupported()
811-
? FSEventsWatcher
812-
: NodeWatcher;
806+
? FSEventsWatcher
807+
: NodeWatcher;
813808

814809
const extensions = this._options.extensions;
815810
const ignorePattern = this._options.ignorePattern;
@@ -907,7 +902,7 @@ class HasteMap extends EventEmitter {
907902
}
908903

909904
const add = () => {
910-
eventsQueue.push({filePath, stat, type});
905+
eventsQueue.push({ filePath, stat, type });
911906
return null;
912907
};
913908

@@ -967,7 +962,7 @@ class HasteMap extends EventEmitter {
967962
hasteMap.map,
968963
hasteMap.mocks,
969964
filePath,
970-
{forceInBand: true},
965+
{ forceInBand: true },
971966
);
972967
// Cleanup
973968
this._cleanup();

packages/jest-haste-map/src/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
*/
77

88
import * as fs from 'fs';
9-
import {Config} from '@jest/types';
9+
import { Config } from '@jest/types';
1010
import ModuleMap from './ModuleMap';
1111
import HasteFS from './HasteFS';
1212

1313
export type IgnoreMatcher = (item: string) => boolean;
14-
export type Mapper = (item: string) => Array<string> | null;
1514

1615
export type WorkerMessage = {
1716
computeDependencies: boolean;
@@ -35,7 +34,6 @@ export type CrawlerOptions = {
3534
extensions: Array<string>;
3635
forceNodeFilesystemAPI: boolean;
3736
ignore: IgnoreMatcher;
38-
mapper?: Mapper | null;
3937
rootDir: string;
4038
roots: Array<string>;
4139
};
@@ -84,7 +82,7 @@ export type RawModuleMap = {
8482
mocks: MockData;
8583
};
8684

87-
type ModuleMapItem = {[platform: string]: ModuleMetaData};
85+
type ModuleMapItem = { [platform: string]: ModuleMetaData };
8886
export type ModuleMetaData = [Config.Path, /* type */ number];
8987

9088
export type HType = {

0 commit comments

Comments
 (0)