From 805a913c96a9fc64852b98e61096bc71760e69e7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 23 Oct 2020 19:35:09 +0200 Subject: [PATCH] chore: migrate jest-resolve to ESM --- CHANGELOG.md | 1 + .../moduleNameMapper.test.ts.snap | 4 +-- .../resolveNoFileExtensions.test.ts.snap | 2 +- .../src/__tests__/normalize.test.js | 33 +++++++++---------- packages/jest-config/src/normalize.ts | 2 +- packages/jest-config/src/utils.ts | 2 +- packages/jest-core/src/watch.ts | 2 +- packages/jest-reporters/src/types.ts | 4 +-- .../src/__tests__/dependency_resolver.test.ts | 2 +- .../jest-resolve-dependencies/src/index.ts | 6 ++-- .../src/__tests__/resolve.test.ts | 6 ++-- packages/jest-resolve/src/defaultResolver.ts | 4 +-- packages/jest-resolve/src/index.ts | 19 +++++------ packages/jest-runner/src/runTest.ts | 6 ++-- packages/jest-runner/src/testWorker.ts | 4 +-- packages/jest-runner/src/types.ts | 4 +-- packages/jest-runtime/src/index.ts | 2 +- packages/jest-runtime/src/types.ts | 4 +-- 18 files changed, 52 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef1f8926045a..58df948af024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Chore & Maintenance - `[*]` [**BREAKING**] Only support Node LTS releases and Node 15 ([#10685](https://github.com/facebook/jest/pull/10685)) +- `[jest-resolve]` [**BREAKING**] Migrate to ESM ([#10688](https://github.com/facebook/jest/pull/10688)) ### Performance diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap index 02fc61173a56..1ad7a5de2304 100644 --- a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap +++ b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap @@ -41,7 +41,7 @@ FAIL __tests__/index.js 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:551:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:556:17) at Object.require (index.js:10:1) `; @@ -70,6 +70,6 @@ FAIL __tests__/index.js 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:551:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:556:17) at Object.require (index.js:10:1) `; diff --git a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap index b292be717f4c..bb8b88769e7c 100644 --- a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap +++ b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap @@ -37,6 +37,6 @@ FAIL __tests__/test.js | ^ 9 | - at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:306:11) + at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:311:11) at Object.require (index.js:8:18) `; diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index ae3b198b2fed..f43174c43757 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -16,7 +16,6 @@ import normalize from '../normalize'; const DEFAULT_CSS_PATTERN = '\\.(css)$'; jest - .mock('jest-resolve') .mock('path', () => jest.requireActual('path').posix) .mock('graceful-fs', () => { const realFs = jest.requireActual('fs'); @@ -56,7 +55,7 @@ beforeEach(() => { expectedPathAbs = path.join(root, 'an', 'abs', 'path'); expectedPathAbsAnother = path.join(root, 'another', 'abs', 'path'); - require('jest-resolve').findNodeModule = findNodeModule; + require('jest-resolve').default.findNodeModule = findNodeModule; jest.spyOn(console, 'warn'); }); @@ -305,7 +304,7 @@ describe('roots', () => { describe('transform', () => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => name); }); @@ -351,7 +350,7 @@ describe('transform', () => { describe('haste', () => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => name); }); @@ -375,7 +374,7 @@ describe('haste', () => { describe('setupFilesAfterEnv', () => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => name.startsWith('/') ? name : '/root/path/foo' + path.sep + name, ); @@ -423,7 +422,7 @@ describe('setupTestFrameworkScriptFile', () => { beforeEach(() => { console.warn.mockImplementation(() => {}); - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => name.startsWith('/') ? name : '/root/path/foo' + path.sep + name, ); @@ -672,7 +671,7 @@ describe('testRunner', () => { }); it('is overwritten by argv', () => { - const Resolver = require('jest-resolve'); + const Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => name); const {options} = normalize( { @@ -703,7 +702,7 @@ describe('coverageDirectory', () => { describe('testEnvironment', () => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => { if (['jsdom', 'jest-environment-jsdom'].includes(name)) { return `node_modules/${name}`; @@ -757,7 +756,7 @@ describe('testEnvironment', () => { describe('babel-jest', () => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => name.indexOf('babel-jest') === -1 ? path.sep + 'node_modules' + path.sep + name @@ -798,7 +797,7 @@ describe('Upgrade help', () => { beforeEach(() => { console.warn.mockImplementation(() => {}); - const Resolver = require('jest-resolve'); + const Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => { if (name == 'bar/baz') { return '/node_modules/bar/baz'; @@ -943,7 +942,7 @@ describe('moduleDirectories', () => { describe('preset', () => { beforeEach(() => { - const Resolver = require('jest-resolve'); + const Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => { if (name === 'react-native/jest-preset') { return '/node_modules/react-native/jest-preset.json'; @@ -1110,7 +1109,7 @@ describe('preset', () => { }); test('searches for .json and .js preset files', () => { - const Resolver = require('jest-resolve'); + const Resolver = require('jest-resolve').default; normalize( { @@ -1224,7 +1223,7 @@ describe('preset', () => { describe('preset with globals', () => { beforeEach(() => { - const Resolver = require('jest-resolve'); + const Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => { if (name === 'global-foo/jest-preset') { return '/node_modules/global-foo/jest-preset.json'; @@ -1285,7 +1284,7 @@ describe.each(['setupFiles', 'setupFilesAfterEnv'])( configKey => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn( name => path.sep + 'node_modules' + path.sep + name, ); @@ -1326,7 +1325,7 @@ describe.each(['setupFiles', 'setupFilesAfterEnv'])( describe('runner', () => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => { if (['eslint', 'jest-runner-eslint', 'my-runner-foo'].includes(name)) { return `node_modules/${name}`; @@ -1384,7 +1383,7 @@ describe('runner', () => { describe('watchPlugins', () => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => { if ( ['typeahead', 'jest-watch-typeahead', 'my-watch-plugin'].includes(name) @@ -1518,7 +1517,7 @@ describe('testPathPattern', () => { describe('win32', () => { beforeEach(() => { jest.mock('path', () => jest.requireActual('path').win32); - require('jest-resolve').findNodeModule = findNodeModule; + require('jest-resolve').default.findNodeModule = findNodeModule; }); afterEach(() => { diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 82da0cc8a397..97ea8dc7bfe2 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -14,7 +14,7 @@ import {statSync} from 'graceful-fs'; import micromatch = require('micromatch'); import type {Config} from '@jest/types'; import {replacePathSepForRegex} from 'jest-regex-util'; -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import {clearLine, replacePathSepForGlob, tryRealpath} from 'jest-util'; import {ValidationError, validate} from 'jest-validate'; import DEFAULT_CONFIG from './Defaults'; diff --git a/packages/jest-config/src/utils.ts b/packages/jest-config/src/utils.ts index e763b94267cc..b931c78f914f 100644 --- a/packages/jest-config/src/utils.ts +++ b/packages/jest-config/src/utils.ts @@ -8,7 +8,7 @@ import * as path from 'path'; import chalk = require('chalk'); import type {Config} from '@jest/types'; -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import {ValidationError} from 'jest-validate'; type ResolveOptions = { diff --git a/packages/jest-core/src/watch.ts b/packages/jest-core/src/watch.ts index 5704f735e14d..b36c3a0b1332 100644 --- a/packages/jest-core/src/watch.ts +++ b/packages/jest-core/src/watch.ts @@ -13,7 +13,7 @@ import slash = require('slash'); import type {Config} from '@jest/types'; import HasteMap = require('jest-haste-map'); import {formatExecError} from 'jest-message-util'; -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import type {Context} from 'jest-runtime'; import {isInteractive, preRunMessage, specialChars} from 'jest-util'; import {ValidationError} from 'jest-validate'; diff --git a/packages/jest-reporters/src/types.ts b/packages/jest-reporters/src/types.ts index 5b54f7c80dfe..d4040ed1f201 100644 --- a/packages/jest-reporters/src/types.ts +++ b/packages/jest-reporters/src/types.ts @@ -13,7 +13,7 @@ import type { } from '@jest/test-result'; import type {Config} from '@jest/types'; import type {FS as HasteFS, ModuleMap} from 'jest-haste-map'; -import type {ResolverType} from 'jest-resolve'; +import type Resolver from 'jest-resolve'; import type {worker} from './CoverageWorker'; export type ReporterOnStartOptions = { @@ -25,7 +25,7 @@ export type Context = { config: Config.ProjectConfig; hasteFS: HasteFS; moduleMap: ModuleMap; - resolver: ResolverType; + resolver: Resolver; }; export type Test = { diff --git a/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts b/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts index 0eff9cf84064..eeacf0056acc 100644 --- a/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts +++ b/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts @@ -9,7 +9,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; import {makeProjectConfig} from '@jest/test-utils'; import type {Config} from '@jest/types'; -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import {buildSnapshotResolver} from 'jest-snapshot'; import DependencyResolver from '../index'; diff --git a/packages/jest-resolve-dependencies/src/index.ts b/packages/jest-resolve-dependencies/src/index.ts index 19228328d7a7..64bc1a406130 100644 --- a/packages/jest-resolve-dependencies/src/index.ts +++ b/packages/jest-resolve-dependencies/src/index.ts @@ -8,7 +8,7 @@ import * as path from 'path'; import type {Config} from '@jest/types'; import type {FS as HasteFS} from 'jest-haste-map'; -import type {ResolveModuleConfig, ResolverType} from 'jest-resolve'; +import type {ResolveModuleConfig, default as Resolver} from 'jest-resolve'; import {SnapshotResolver, isSnapshotPath} from 'jest-snapshot'; declare namespace DependencyResolver { @@ -24,11 +24,11 @@ declare namespace DependencyResolver { */ class DependencyResolver { private _hasteFS: HasteFS; - private _resolver: ResolverType; + private _resolver: Resolver; private _snapshotResolver: SnapshotResolver; constructor( - resolver: ResolverType, + resolver: Resolver, hasteFS: HasteFS, snapshotResolver: SnapshotResolver, ) { diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index cd1bee69ac80..0a7584df4f70 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -10,7 +10,7 @@ import * as path from 'path'; import * as fs from 'graceful-fs'; import {sync as resolveSync} from 'resolve'; import {ModuleMap} from 'jest-haste-map'; -import Resolver = require('../'); +import Resolver from '../'; import userResolver from '../__mocks__/userResolver'; import defaultResolver from '../defaultResolver'; import nodeModulesPaths from '../nodeModulesPaths'; @@ -286,7 +286,7 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => { it('can resolve node modules relative to absolute paths in "moduleDirectories" on Windows platforms', () => { jest.doMock('path', () => _path.win32); const path = require('path'); - const Resolver = require('../'); + const Resolver = require('../').default; const cwd = 'D:\\temp\\project'; const src = 'C:\\path\\to\\node_modules'; @@ -306,7 +306,7 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => { it('can resolve node modules relative to absolute paths in "moduleDirectories" on Posix platforms', () => { jest.doMock('path', () => _path.posix); const path = require('path'); - const Resolver = require('../'); + const Resolver = require('../').default; const cwd = '/temp/project'; const src = '/path/to/node_modules'; diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 3f15929f1a8e..f4b2097ebe5e 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -23,11 +23,11 @@ type ResolverOptions = { packageFilter?: ResolveOpts['packageFilter']; }; +// https://github.com/facebook/jest/pull/10617 declare global { namespace NodeJS { export interface ProcessVersions { - // the "pnp" version named isn't in DefinitelyTyped - pnp?: unknown; + pnp?: any; } } } diff --git a/packages/jest-resolve/src/index.ts b/packages/jest-resolve/src/index.ts index cec294fcf5e2..bc5610a21431 100644 --- a/packages/jest-resolve/src/index.ts +++ b/packages/jest-resolve/src/index.ts @@ -31,16 +31,13 @@ type FindNodeModuleConfig = { throwIfNotFound?: boolean; }; -// TODO: replace with a Map in Jest 26 +// TODO: replace with a Map in Jest 27 type BooleanObject = Record; -declare namespace Resolver { - export type ResolveModuleConfig = { - skipNodeResolution?: boolean; - paths?: Array; - }; - export type ResolverType = Resolver; -} +export type ResolveModuleConfig = { + skipNodeResolution?: boolean; + paths?: Array; +}; const NATIVE_PLATFORM = 'native'; @@ -139,7 +136,7 @@ class Resolver { resolveModuleFromDirIfExists( dirname: Config.Path, moduleName: string, - options?: Resolver.ResolveModuleConfig, + options?: ResolveModuleConfig, ): Config.Path | null { const paths = (options && options.paths) || this._options.modulePaths; const moduleDirectory = this._options.moduleDirectories; @@ -227,7 +224,7 @@ class Resolver { resolveModule( from: Config.Path, moduleName: string, - options?: Resolver.ResolveModuleConfig, + options?: ResolveModuleConfig, ): Config.Path { const dirname = path.dirname(from); const module = @@ -502,4 +499,4 @@ Please check your configuration for these entries: return error; }; -export = Resolver; +export default Resolver; diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 4770bf5369c5..5bf19b21a0da 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -24,7 +24,7 @@ import {getTestEnvironment} from 'jest-config'; import * as docblock from 'jest-docblock'; import LeakDetector from 'jest-leak-detector'; import {formatExecError} from 'jest-message-util'; -import type {ResolverType} from 'jest-resolve'; +import type Resolver from 'jest-resolve'; import RuntimeClass = require('jest-runtime'); import {ErrorWithStack, interopRequireDefault, setGlobal} from 'jest-util'; import type {TestFileEvent, TestFramework, TestRunnerContext} from './types'; @@ -79,7 +79,7 @@ async function runTestInternal( path: Config.Path, globalConfig: Config.GlobalConfig, config: Config.ProjectConfig, - resolver: ResolverType, + resolver: Resolver, context?: TestRunnerContext, sendMessageToJest?: TestFileEvent, ): Promise { @@ -331,7 +331,7 @@ export default async function runTest( path: Config.Path, globalConfig: Config.GlobalConfig, config: Config.ProjectConfig, - resolver: ResolverType, + resolver: Resolver, context?: TestRunnerContext, sendMessageToJest?: TestFileEvent, ): Promise { diff --git a/packages/jest-runner/src/testWorker.ts b/packages/jest-runner/src/testWorker.ts index 263d3994cb58..2284a856fcb3 100644 --- a/packages/jest-runner/src/testWorker.ts +++ b/packages/jest-runner/src/testWorker.ts @@ -11,7 +11,7 @@ import type {SerializableError, TestResult} from '@jest/test-result'; import type {Config} from '@jest/types'; import HasteMap = require('jest-haste-map'); import {separateMessageFromStack} from 'jest-message-util'; -import type {ResolverType} from 'jest-resolve'; +import type Resolver from 'jest-resolve'; import Runtime = require('jest-runtime'); import {messageParent} from 'jest-worker'; import runTest from './runTest'; @@ -57,7 +57,7 @@ const formatError = (error: string | ErrorWithCode): SerializableError => { }; }; -const resolvers = new Map(); +const resolvers = new Map(); const getResolver = (config: Config.ProjectConfig) => { const resolver = resolvers.get(config.name); if (!resolver) { diff --git a/packages/jest-runner/src/types.ts b/packages/jest-runner/src/types.ts index 351770603241..f90be3e2c201 100644 --- a/packages/jest-runner/src/types.ts +++ b/packages/jest-runner/src/types.ts @@ -14,7 +14,7 @@ import type { } from '@jest/test-result'; import type {Config} from '@jest/types'; import type {FS as HasteFS, ModuleMap} from 'jest-haste-map'; -import type {ResolverType} from 'jest-resolve'; +import type Resolver from 'jest-resolve'; import type {RuntimeType} from 'jest-runtime'; export type ErrorWithCode = Error & {code?: string}; @@ -28,7 +28,7 @@ export type Context = { config: Config.ProjectConfig; hasteFS: HasteFS; moduleMap: ModuleMap; - resolver: ResolverType; + resolver: Resolver; }; export type OnTestStart = (test: Test) => Promise; diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index f9c0cb9ac9c4..09eb75d3c82a 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -45,7 +45,7 @@ import HasteMap = require('jest-haste-map'); import {formatStackTrace, separateMessageFromStack} from 'jest-message-util'; import jestMock = require('jest-mock'); import {escapePathForRegex} from 'jest-regex-util'; -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import Snapshot = require('jest-snapshot'); import {createDirectory, deepCyclicCopy} from 'jest-util'; import {run as cliRun} from './cli'; diff --git a/packages/jest-runtime/src/types.ts b/packages/jest-runtime/src/types.ts index 744b24dc7c15..b78f08090446 100644 --- a/packages/jest-runtime/src/types.ts +++ b/packages/jest-runtime/src/types.ts @@ -7,11 +7,11 @@ import type {Config} from '@jest/types'; import type {FS as HasteFS, ModuleMap} from 'jest-haste-map'; -import type {ResolverType} from 'jest-resolve'; +import type Resolver from 'jest-resolve'; export type Context = { config: Config.ProjectConfig; hasteFS: HasteFS; moduleMap: ModuleMap; - resolver: ResolverType; + resolver: Resolver; };