Skip to content

Commit 7edfb10

Browse files
authored
chore: migrate jest-haste-map to ESM (#10875)
1 parent daf4d02 commit 7edfb10

File tree

13 files changed

+36
-45
lines changed

13 files changed

+36
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- `[jest-config]` [**BREAKING**] Remove `enabledTestsMap` config, use `filter` instead ([#10787](https://github.com/facebook/jest/pull/10787))
3838
- `[jest-console]` [**BREAKING**] Move `root` into `config` and take `GlobalConfig` as mandatory parameter for `getConsoleOutput` ([#10126](https://github.com/facebook/jest/pull/10126))
3939
- `[jest-fake-timers]` Clarify global behavior of `jest.useFakeTimers` and `jest.useRealTimers` ([#10867](https://github.com/facebook/jest/pull/10867))
40+
- `[jest-haste-map]` [**BREAKING**] Migrate to ESM ([#10875](https://github.com/facebook/jest/pull/10875))
4041
- `[jest-repl, jest-runtime]` [**BREAKING**] Move the `jest-runtime` CLI into `jest-repl` ([#10016](https://github.com/facebook/jest/pull/10016))
4142
- `[jest-resolve]` [**BREAKING**] Migrate to ESM ([#10688](https://github.com/facebook/jest/pull/10688))
4243
- `[jest-resolve-dependencies]` [**BREAKING**] Migrate to ESM ([#10876](https://github.com/facebook/jest/pull/10876))

e2e/__tests__/hasteMapMockChanged.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import * as path from 'path';
9-
import JestHasteMap = require('jest-haste-map');
9+
import JestHasteMap from 'jest-haste-map';
1010
import {cleanup, writeFiles} from '../Utils';
1111

1212
// Directory must be here for Watchman to be enabled.

e2e/__tests__/hasteMapSha1.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import {tmpdir} from 'os';
99
import * as path from 'path';
10-
import JestHasteMap = require('jest-haste-map');
10+
import JestHasteMap from 'jest-haste-map';
1111
import {cleanup, writeFiles} from '../Utils';
1212

1313
const DIR = path.resolve(tmpdir(), 'haste_map_sha1');

e2e/__tests__/hasteMapSize.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import {tmpdir} from 'os';
99
import * as path from 'path';
1010
import {realpathSync} from 'graceful-fs';
11-
import HasteMap = require('jest-haste-map');
11+
import HasteMap from 'jest-haste-map';
1212
import {cleanup, writeFiles} from '../Utils';
1313

1414
const DIR = path.resolve(realpathSync.native(tmpdir()), 'haste_map_size');

packages/jest-core/src/__tests__/watchFileChanges.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as fs from 'graceful-fs';
1212
import rimraf = require('rimraf');
1313
import type {AggregatedResult} from '@jest/test-result';
1414
import {normalize} from 'jest-config';
15-
import HasteMap = require('jest-haste-map');
15+
import type HasteMap from 'jest-haste-map';
1616
import Runtime from 'jest-runtime';
1717
import {JestHook} from 'jest-watcher';
1818

packages/jest-core/src/cli/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {AggregatedResult} from '@jest/test-result';
1313
import type {Config} from '@jest/types';
1414
import type {ChangedFilesPromise} from 'jest-changed-files';
1515
import {readConfigs} from 'jest-config';
16-
import HasteMap = require('jest-haste-map');
16+
import type HasteMap from 'jest-haste-map';
1717
import Runtime, {Context} from 'jest-runtime';
1818
import {createDirectory, preRunMessage} from 'jest-util';
1919
import TestWatcher from '../TestWatcher';

packages/jest-core/src/watch.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import chalk = require('chalk');
1111
import exit = require('exit');
1212
import slash = require('slash');
1313
import type {Config} from '@jest/types';
14-
import HasteMap = require('jest-haste-map');
14+
import type {
15+
ChangeEvent as HasteChangeEvent,
16+
default as HasteMap,
17+
} from 'jest-haste-map';
1518
import {formatExecError} from 'jest-message-util';
1619
import Resolver from 'jest-resolve';
1720
import type {Context} from 'jest-runtime';
@@ -234,7 +237,7 @@ export default function watch(
234237
hasteMapInstances.forEach((hasteMapInstance, index) => {
235238
hasteMapInstance.on(
236239
'change',
237-
({eventsQueue, hasteFS, moduleMap}: HasteMap.HasteChangeEvent) => {
240+
({eventsQueue, hasteFS, moduleMap}: HasteChangeEvent) => {
238241
const validPaths = eventsQueue.filter(({filePath}) =>
239242
isValidPath(globalConfig, filePath),
240243
);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ describe('HasteMap', () => {
192192
console.warn = jest.fn();
193193
console.error = jest.fn();
194194

195-
HasteMap = require('../');
195+
HasteMap = require('../').default;
196196
H = HasteMap.H;
197197

198198
getCacheFilePath = HasteMap.getCacheFilePath;
@@ -225,7 +225,7 @@ describe('HasteMap', () => {
225225

226226
it('creates valid cache file paths', () => {
227227
jest.resetModules();
228-
HasteMap = require('../');
228+
HasteMap = require('../').default;
229229

230230
expect(
231231
HasteMap.getCacheFilePath('/', '@scoped/package', 'random-value'),
@@ -238,15 +238,15 @@ describe('HasteMap', () => {
238238

239239
it('creates different cache file paths for different roots', () => {
240240
jest.resetModules();
241-
const HasteMap = require('../');
241+
const HasteMap = require('../').default;
242242
const hasteMap1 = new HasteMap({...defaultConfig, rootDir: '/root1'});
243243
const hasteMap2 = new HasteMap({...defaultConfig, rootDir: '/root2'});
244244
expect(hasteMap1.getCacheFilePath()).not.toBe(hasteMap2.getCacheFilePath());
245245
});
246246

247247
it('creates different cache file paths for different dependency extractor cache keys', () => {
248248
jest.resetModules();
249-
const HasteMap = require('../');
249+
const HasteMap = require('../').default;
250250
const dependencyExtractor = require('./dependencyExtractor');
251251
const config = {
252252
...defaultConfig,
@@ -261,7 +261,7 @@ describe('HasteMap', () => {
261261

262262
it('creates different cache file paths for different hasteImplModulePath cache keys', () => {
263263
jest.resetModules();
264-
const HasteMap = require('../');
264+
const HasteMap = require('../').default;
265265
const hasteImpl = require('./haste_impl');
266266
hasteImpl.setCacheKey('foo');
267267
const hasteMap1 = new HasteMap(defaultConfig);
@@ -272,7 +272,7 @@ describe('HasteMap', () => {
272272

273273
it('creates different cache file paths for different projects', () => {
274274
jest.resetModules();
275-
const HasteMap = require('../');
275+
const HasteMap = require('../').default;
276276
const hasteMap1 = new HasteMap({...defaultConfig, name: '@scoped/package'});
277277
const hasteMap2 = new HasteMap({...defaultConfig, name: '-scoped-package'});
278278
expect(hasteMap1.getCacheFilePath()).not.toBe(hasteMap2.getCacheFilePath());

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ import {escapePathForRegex} from 'jest-regex-util';
1919
import serializer from 'jest-serializer';
2020
import Worker from 'jest-worker';
2121
import HasteFS from './HasteFS';
22-
import HasteModuleMap, {
23-
SerializableModuleMap as HasteSerializableModuleMap,
24-
} from './ModuleMap';
22+
import HasteModuleMap from './ModuleMap';
2523
import H from './constants';
2624
import nodeCrawl = require('./crawlers/node');
2725
import watchmanCrawl = require('./crawlers/watchman');
@@ -51,8 +49,6 @@ import {getSha1, worker} from './worker';
5149
// understand `require`.
5250
const {version: VERSION} = require('../package.json');
5351

54-
type HType = typeof H;
55-
5652
type Options = {
5753
cacheDirectory?: string;
5854
computeDependencies?: boolean;
@@ -106,14 +102,10 @@ type Watcher = {
106102

107103
type WorkerInterface = {worker: typeof worker; getSha1: typeof getSha1};
108104

109-
// TODO: Ditch namespace when this module exports ESM
110-
declare namespace HasteMap {
111-
export type ModuleMap = HasteModuleMap;
112-
export type SerializableModuleMap = HasteSerializableModuleMap;
113-
export type FS = HasteFS;
114-
export type HasteMapObject = InternalHasteMapObject;
115-
export type HasteChangeEvent = ChangeEvent;
116-
}
105+
export {default as ModuleMap} from './ModuleMap';
106+
export type {SerializableModuleMap} from './ModuleMap';
107+
export type {default as FS} from './HasteFS';
108+
export type {ChangeEvent, HasteMap as HasteMapObject} from './types';
117109

118110
const CHANGE_INTERVAL = 30;
119111
const MAX_WAIT_TIME = 240000;
@@ -215,7 +207,7 @@ function invariant(condition: unknown, message?: string): asserts condition {
215207
* Worker processes can directly access the cache through `HasteMap.read()`.
216208
*
217209
*/
218-
class HasteMap extends EventEmitter {
210+
export default class HasteMap extends EventEmitter {
219211
private _buildPromise: Promise<InternalHasteMapObject> | null;
220212
private _cachePath: Config.Path;
221213
private _changeInterval?: NodeJS.Timeout;
@@ -1093,12 +1085,10 @@ class HasteMap extends EventEmitter {
10931085
};
10941086
}
10951087

1096-
static H: HType;
1097-
static DuplicateError: typeof DuplicateError;
1098-
static ModuleMap: typeof HasteModuleMap;
1088+
static H = H;
10991089
}
11001090

1101-
class DuplicateError extends Error {
1091+
export class DuplicateError extends Error {
11021092
mockPath1: string;
11031093
mockPath2: string;
11041094

@@ -1117,9 +1107,3 @@ function copy<T extends Record<string, unknown>>(object: T): T {
11171107
function copyMap<K, V>(input: Map<K, V>): Map<K, V> {
11181108
return new Map(input);
11191109
}
1120-
1121-
HasteMap.H = H;
1122-
HasteMap.DuplicateError = DuplicateError;
1123-
HasteMap.ModuleMap = HasteModuleMap;
1124-
1125-
export = HasteMap;

packages/jest-runner/src/testWorker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import exit = require('exit');
1010
import type {SerializableError, TestResult} from '@jest/test-result';
1111
import type {Config} from '@jest/types';
12-
import HasteMap = require('jest-haste-map');
12+
import {ModuleMap, SerializableModuleMap} from 'jest-haste-map';
1313
import {separateMessageFromStack} from 'jest-message-util';
1414
import type Resolver from 'jest-resolve';
1515
import Runtime from 'jest-runtime';
@@ -23,7 +23,7 @@ import type {
2323

2424
export type SerializableResolver = {
2525
config: Config.ProjectConfig;
26-
serializableModuleMap: HasteMap.SerializableModuleMap;
26+
serializableModuleMap: SerializableModuleMap;
2727
};
2828

2929
type WorkerData = {
@@ -74,7 +74,7 @@ export function setup(setupData: {
7474
config,
7575
serializableModuleMap,
7676
} of setupData.serializableResolvers) {
77-
const moduleMap = HasteMap.ModuleMap.fromJSON(serializableModuleMap);
77+
const moduleMap = ModuleMap.fromJSON(serializableModuleMap);
7878
resolvers.set(config.name, Runtime.createResolver(config, moduleMap));
7979
}
8080
}

0 commit comments

Comments
 (0)