Skip to content

Commit a867348

Browse files
committed
chore: migrate jest-haste-map to ESM
1 parent 034cd34 commit a867348

File tree

14 files changed

+37
-44
lines changed

14 files changed

+37
-44
lines changed

CHANGELOG.md

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

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 & 22 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');
@@ -106,14 +104,10 @@ type Watcher = {
106104

107105
type WorkerInterface = {worker: typeof worker; getSha1: typeof getSha1};
108106

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-
}
107+
export {default as ModuleMap} from './ModuleMap';
108+
export type {SerializableModuleMap} from './ModuleMap';
109+
export type {default as FS} from './HasteFS';
110+
export type {ChangeEvent, HasteMap as HasteMapObject} from './types';
117111

118112
const CHANGE_INTERVAL = 30;
119113
const MAX_WAIT_TIME = 240000;
@@ -215,7 +209,7 @@ function invariant(condition: unknown, message?: string): asserts condition {
215209
* Worker processes can directly access the cache through `HasteMap.read()`.
216210
*
217211
*/
218-
class HasteMap extends EventEmitter {
212+
export default class HasteMap extends EventEmitter {
219213
private _buildPromise: Promise<InternalHasteMapObject> | null;
220214
private _cachePath: Config.Path;
221215
private _changeInterval?: NodeJS.Timeout;
@@ -1093,12 +1087,10 @@ class HasteMap extends EventEmitter {
10931087
};
10941088
}
10951089

1096-
static H: HType;
1097-
static DuplicateError: typeof DuplicateError;
1098-
static ModuleMap: typeof HasteModuleMap;
1090+
static H: HType = H;
10991091
}
11001092

1101-
class DuplicateError extends Error {
1093+
export class DuplicateError extends Error {
11021094
mockPath1: string;
11031095
mockPath2: string;
11041096

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

packages/jest-resolve/src/__tests__/resolve.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import * as path from 'path';
1010
import * as fs from 'graceful-fs';
1111
import {sync as resolveSync} from 'resolve';
12-
import {ModuleMap} from 'jest-haste-map';
12+
import HasteMap, {ModuleMap} from 'jest-haste-map';
1313
import Resolver from '../';
1414
import userResolver from '../__mocks__/userResolver';
1515
import defaultResolver from '../defaultResolver';

0 commit comments

Comments
 (0)