Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate jest-haste-map to ESM #10875

Merged
merged 1 commit into from Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -36,6 +36,7 @@
- `[jest-config]` [**BREAKING**] Remove `enabledTestsMap` config, use `filter` instead ([#10787](https://github.com/facebook/jest/pull/10787))
- `[jest-console]` [**BREAKING**] Move `root` into `config` and take `GlobalConfig` as mandatory parameter for `getConsoleOutput` ([#10126](https://github.com/facebook/jest/pull/10126))
- `[jest-fake-timers]` Clarify global behavior of `jest.useFakeTimers` and `jest.useRealTimers` ([#10867](https://github.com/facebook/jest/pull/10867))
- `[jest-haste-map]` [**BREAKING**] Migrate to ESM ([#10875](https://github.com/facebook/jest/pull/10875))
- `[jest-repl, jest-runtime]` [**BREAKING**] Move the `jest-runtime` CLI into `jest-repl` ([#10016](https://github.com/facebook/jest/pull/10016))
- `[jest-resolve]` [**BREAKING**] Migrate to ESM ([#10688](https://github.com/facebook/jest/pull/10688))
- `[jest-resolve-dependencies]` [**BREAKING**] Migrate to ESM ([#10876](https://github.com/facebook/jest/pull/10876))
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/hasteMapMockChanged.test.ts
Expand Up @@ -6,7 +6,7 @@
*/

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

// Directory must be here for Watchman to be enabled.
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/hasteMapSha1.test.ts
Expand Up @@ -7,7 +7,7 @@

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

const DIR = path.resolve(tmpdir(), 'haste_map_sha1');
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/hasteMapSize.test.ts
Expand Up @@ -8,7 +8,7 @@
import {tmpdir} from 'os';
import * as path from 'path';
import {realpathSync} from 'graceful-fs';
import HasteMap = require('jest-haste-map');
import HasteMap from 'jest-haste-map';
import {cleanup, writeFiles} from '../Utils';

const DIR = path.resolve(realpathSync.native(tmpdir()), 'haste_map_size');
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/__tests__/watchFileChanges.test.ts
Expand Up @@ -12,7 +12,7 @@ import * as fs from 'graceful-fs';
import rimraf = require('rimraf');
import type {AggregatedResult} from '@jest/test-result';
import {normalize} from 'jest-config';
import HasteMap = require('jest-haste-map');
import type HasteMap from 'jest-haste-map';
import Runtime from 'jest-runtime';
import {JestHook} from 'jest-watcher';

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/cli/index.ts
Expand Up @@ -13,7 +13,7 @@ import type {AggregatedResult} from '@jest/test-result';
import type {Config} from '@jest/types';
import type {ChangedFilesPromise} from 'jest-changed-files';
import {readConfigs} from 'jest-config';
import HasteMap = require('jest-haste-map');
import type HasteMap from 'jest-haste-map';
import Runtime, {Context} from 'jest-runtime';
import {createDirectory, preRunMessage} from 'jest-util';
import TestWatcher from '../TestWatcher';
Expand Down
7 changes: 5 additions & 2 deletions packages/jest-core/src/watch.ts
Expand Up @@ -11,7 +11,10 @@ import chalk = require('chalk');
import exit = require('exit');
import slash = require('slash');
import type {Config} from '@jest/types';
import HasteMap = require('jest-haste-map');
import type {
ChangeEvent as HasteChangeEvent,
default as HasteMap,
} from 'jest-haste-map';
import {formatExecError} from 'jest-message-util';
import Resolver from 'jest-resolve';
import type {Context} from 'jest-runtime';
Expand Down Expand Up @@ -234,7 +237,7 @@ export default function watch(
hasteMapInstances.forEach((hasteMapInstance, index) => {
hasteMapInstance.on(
'change',
({eventsQueue, hasteFS, moduleMap}: HasteMap.HasteChangeEvent) => {
({eventsQueue, hasteFS, moduleMap}: HasteChangeEvent) => {
const validPaths = eventsQueue.filter(({filePath}) =>
isValidPath(globalConfig, filePath),
);
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-haste-map/src/__tests__/index.test.js
Expand Up @@ -192,7 +192,7 @@ describe('HasteMap', () => {
console.warn = jest.fn();
console.error = jest.fn();

HasteMap = require('../');
HasteMap = require('../').default;
H = HasteMap.H;

getCacheFilePath = HasteMap.getCacheFilePath;
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('HasteMap', () => {

it('creates valid cache file paths', () => {
jest.resetModules();
HasteMap = require('../');
HasteMap = require('../').default;

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

it('creates different cache file paths for different roots', () => {
jest.resetModules();
const HasteMap = require('../');
const HasteMap = require('../').default;
const hasteMap1 = new HasteMap({...defaultConfig, rootDir: '/root1'});
const hasteMap2 = new HasteMap({...defaultConfig, rootDir: '/root2'});
expect(hasteMap1.getCacheFilePath()).not.toBe(hasteMap2.getCacheFilePath());
});

it('creates different cache file paths for different dependency extractor cache keys', () => {
jest.resetModules();
const HasteMap = require('../');
const HasteMap = require('../').default;
const dependencyExtractor = require('./dependencyExtractor');
const config = {
...defaultConfig,
Expand All @@ -261,7 +261,7 @@ describe('HasteMap', () => {

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

it('creates different cache file paths for different projects', () => {
jest.resetModules();
const HasteMap = require('../');
const HasteMap = require('../').default;
const hasteMap1 = new HasteMap({...defaultConfig, name: '@scoped/package'});
const hasteMap2 = new HasteMap({...defaultConfig, name: '-scoped-package'});
expect(hasteMap1.getCacheFilePath()).not.toBe(hasteMap2.getCacheFilePath());
Expand Down
32 changes: 8 additions & 24 deletions packages/jest-haste-map/src/index.ts
Expand Up @@ -19,9 +19,7 @@ import {escapePathForRegex} from 'jest-regex-util';
import serializer from 'jest-serializer';
import Worker from 'jest-worker';
import HasteFS from './HasteFS';
import HasteModuleMap, {
SerializableModuleMap as HasteSerializableModuleMap,
} from './ModuleMap';
import HasteModuleMap from './ModuleMap';
import H from './constants';
import nodeCrawl = require('./crawlers/node');
import watchmanCrawl = require('./crawlers/watchman');
Expand Down Expand Up @@ -51,8 +49,6 @@ import {getSha1, worker} from './worker';
// understand `require`.
const {version: VERSION} = require('../package.json');

type HType = typeof H;

type Options = {
cacheDirectory?: string;
computeDependencies?: boolean;
Expand Down Expand Up @@ -106,14 +102,10 @@ type Watcher = {

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

// TODO: Ditch namespace when this module exports ESM
declare namespace HasteMap {
export type ModuleMap = HasteModuleMap;
export type SerializableModuleMap = HasteSerializableModuleMap;
export type FS = HasteFS;
export type HasteMapObject = InternalHasteMapObject;
export type HasteChangeEvent = ChangeEvent;
}
export {default as ModuleMap} from './ModuleMap';
export type {SerializableModuleMap} from './ModuleMap';
export type {default as FS} from './HasteFS';
export type {ChangeEvent, HasteMap as HasteMapObject} from './types';

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

static H: HType;
static DuplicateError: typeof DuplicateError;
static ModuleMap: typeof HasteModuleMap;
static H = H;
}

class DuplicateError extends Error {
export class DuplicateError extends Error {
mockPath1: string;
mockPath2: string;

Expand All @@ -1117,9 +1107,3 @@ function copy<T extends Record<string, unknown>>(object: T): T {
function copyMap<K, V>(input: Map<K, V>): Map<K, V> {
return new Map(input);
}

HasteMap.H = H;
HasteMap.DuplicateError = DuplicateError;
HasteMap.ModuleMap = HasteModuleMap;

export = HasteMap;
6 changes: 3 additions & 3 deletions packages/jest-runner/src/testWorker.ts
Expand Up @@ -9,7 +9,7 @@
import exit = require('exit');
import type {SerializableError, TestResult} from '@jest/test-result';
import type {Config} from '@jest/types';
import HasteMap = require('jest-haste-map');
import {ModuleMap, SerializableModuleMap} from 'jest-haste-map';
import {separateMessageFromStack} from 'jest-message-util';
import type Resolver from 'jest-resolve';
import Runtime from 'jest-runtime';
Expand All @@ -23,7 +23,7 @@ import type {

export type SerializableResolver = {
config: Config.ProjectConfig;
serializableModuleMap: HasteMap.SerializableModuleMap;
serializableModuleMap: SerializableModuleMap;
};

type WorkerData = {
Expand Down Expand Up @@ -74,7 +74,7 @@ export function setup(setupData: {
config,
serializableModuleMap,
} of setupData.serializableResolvers) {
const moduleMap = HasteMap.ModuleMap.fromJSON(serializableModuleMap);
const moduleMap = ModuleMap.fromJSON(serializableModuleMap);
resolvers.set(config.name, Runtime.createResolver(config, moduleMap));
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -41,7 +41,7 @@ import {
shouldInstrument,
} from '@jest/transform';
import type {Config, Global} from '@jest/types';
import HasteMap = require('jest-haste-map');
import HasteMap, {ModuleMap} from 'jest-haste-map';
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
import jestMock = require('jest-mock');
import {escapePathForRegex} from 'jest-regex-util';
Expand Down Expand Up @@ -334,7 +334,7 @@ export default class Runtime {

static createResolver(
config: Config.ProjectConfig,
moduleMap: HasteMap.ModuleMap,
moduleMap: ModuleMap,
): Resolver {
return new Resolver(moduleMap, {
defaultPlatform: config.haste.defaultPlatform,
Expand Down
7 changes: 5 additions & 2 deletions packages/jest-test-sequencer/src/index.ts
Expand Up @@ -7,7 +7,7 @@

import * as fs from 'graceful-fs';
import type {AggregatedResult} from '@jest/test-result';
import {getCacheFilePath} from 'jest-haste-map';
import HasteMap from 'jest-haste-map';
import type {Test} from 'jest-runner';
import type {Context} from 'jest-runtime';

Expand Down Expand Up @@ -36,7 +36,10 @@ export default class TestSequencer {

_getCachePath(context: Context): string {
const {config} = context;
return getCacheFilePath(config.cacheDirectory, 'perf-cache-' + config.name);
return HasteMap.getCacheFilePath(
config.cacheDirectory,
'perf-cache-' + config.name,
);
}

_getCache(test: Test): Cache {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -17,7 +17,7 @@ import {addHook} from 'pirates';
import slash = require('slash');
import {sync as writeFileAtomic} from 'write-file-atomic';
import type {Config} from '@jest/types';
import HasteMap = require('jest-haste-map');
import HasteMap from 'jest-haste-map';
import {
createDirectory,
interopRequireDefault,
Expand Down