diff --git a/CHANGELOG.md b/CHANGELOG.md index a3fdc55f111b..d43698c9b78b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ### Chore & Maintenance +- `[jest-resolve]` [**BREAKING**] Migrate to ESM ([#10688](https://github.com/facebook/jest/pull/10688)) + ### Performance ## 26.6.1 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 89ee1bdb2daa..4ab11cbb8aa6 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -17,7 +17,6 @@ import {DEFAULT_JS_PATTERN} from '../constants'; const DEFAULT_CSS_PATTERN = '\\.(css)$'; jest - .mock('jest-resolve') .mock('path', () => jest.requireActual('path').posix) .mock('graceful-fs', () => { const realFs = jest.requireActual('fs'); @@ -57,7 +56,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'); }); @@ -306,7 +305,7 @@ describe('roots', () => { describe('transform', () => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => name); }); @@ -352,7 +351,7 @@ describe('transform', () => { describe('haste', () => { let Resolver; beforeEach(() => { - Resolver = require('jest-resolve'); + Resolver = require('jest-resolve').default; Resolver.findNodeModule = jest.fn(name => name); }); @@ -376,7 +375,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, ); @@ -424,7 +423,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, ); @@ -673,7 +672,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( { @@ -704,7 +703,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}`; @@ -758,7 +757,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 @@ -799,7 +798,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'; @@ -944,7 +943,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'; @@ -1111,7 +1110,7 @@ describe('preset', () => { }); test('searches for .json and .js preset files', () => { - const Resolver = require('jest-resolve'); + const Resolver = require('jest-resolve').default; normalize( { @@ -1225,7 +1224,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'; @@ -1286,7 +1285,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, ); @@ -1327,7 +1326,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}`; @@ -1385,7 +1384,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) @@ -1519,7 +1518,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 5d60d45c2053..d54f423f9ffa 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -14,7 +14,7 @@ import {ValidationError, validate} from 'jest-validate'; import {clearLine, replacePathSepForGlob, tryRealpath} from 'jest-util'; import chalk = require('chalk'); import micromatch = require('micromatch'); -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import {replacePathSepForRegex} from 'jest-regex-util'; import merge = require('deepmerge'); import validatePattern from './validatePattern'; diff --git a/packages/jest-config/src/utils.ts b/packages/jest-config/src/utils.ts index baa834fffcf9..dff6dd2d7f48 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 type {Config} from '@jest/types'; import {ValidationError} from 'jest-validate'; -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import chalk = require('chalk'); type ResolveOptions = { diff --git a/packages/jest-core/src/watch.ts b/packages/jest-core/src/watch.ts index 64d9407fefe6..a52f4b2789f3 100644 --- a/packages/jest-core/src/watch.ts +++ b/packages/jest-core/src/watch.ts @@ -15,7 +15,7 @@ import {formatExecError} from 'jest-message-util'; import {isInteractive, preRunMessage, specialChars} from 'jest-util'; import {ValidationError} from 'jest-validate'; import type {Context} from 'jest-runtime'; -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import type {Config} from '@jest/types'; import { AllowedConfigOptions, diff --git a/packages/jest-reporters/src/types.ts b/packages/jest-reporters/src/types.ts index 45dc7c5c00f6..5c4a868ccb54 100644 --- a/packages/jest-reporters/src/types.ts +++ b/packages/jest-reporters/src/types.ts @@ -13,7 +13,7 @@ import type { TestResult, } from '@jest/test-result'; 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 './coverage_worker'; 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 d32526f3fd81..73cb219d8a9a 100644 --- a/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts +++ b/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts @@ -7,7 +7,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import type {Config} from '@jest/types'; import {buildSnapshotResolver} from 'jest-snapshot'; import {makeProjectConfig} from '../../../../TestUtils'; diff --git a/packages/jest-resolve-dependencies/src/index.ts b/packages/jest-resolve-dependencies/src/index.ts index c354ed76efc6..880ead0cd661 100644 --- a/packages/jest-resolve-dependencies/src/index.ts +++ b/packages/jest-resolve-dependencies/src/index.ts @@ -7,7 +7,7 @@ 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'; namespace DependencyResolver { @@ -23,11 +23,11 @@ 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 822cdede4d2d..598fea807bdb 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 {ModuleMap} from 'jest-haste-map'; import {sync as resolveSync} from 'resolve'; -import Resolver = require('../'); +import Resolver from '../'; import userResolver from '../__mocks__/userResolver'; import nodeModulesPaths from '../nodeModulesPaths'; import defaultResolver from '../defaultResolver'; @@ -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 c925ae8fe3b0..68b8d6da3a13 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 2068632f84c1..6233aa1084d4 100644 --- a/packages/jest-resolve/src/index.ts +++ b/packages/jest-resolve/src/index.ts @@ -34,13 +34,10 @@ type FindNodeModuleConfig = { // TODO: replace with a Map in Jest 26 type BooleanObject = Record; -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 85aadd468329..f0b94b89dea5 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -21,7 +21,7 @@ import RuntimeClass = require('jest-runtime'); import * as fs from 'graceful-fs'; import {ErrorWithStack, interopRequireDefault, setGlobal} from 'jest-util'; import LeakDetector from 'jest-leak-detector'; -import type {ResolverType} from 'jest-resolve'; +import type Resolver from 'jest-resolve'; import {getTestEnvironment} from 'jest-config'; import * as docblock from 'jest-docblock'; import {formatExecError} from 'jest-message-util'; @@ -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 10e2e3e0b318..e01ec33c6217 100644 --- a/packages/jest-runner/src/testWorker.ts +++ b/packages/jest-runner/src/testWorker.ts @@ -12,7 +12,7 @@ import HasteMap = require('jest-haste-map'); import exit = require('exit'); import {separateMessageFromStack} from 'jest-message-util'; import Runtime = require('jest-runtime'); -import type {ResolverType} from 'jest-resolve'; +import type Resolver from 'jest-resolve'; import {messageParent} from 'jest-worker'; import type { ErrorWithCode, @@ -58,7 +58,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 4b20d260e910..d090c1ecdf5b 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 {JestEnvironment} from '@jest/environment'; 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 224fe024d25c..14f51ba74c70 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -45,7 +45,7 @@ import {CoverageInstrumenter, V8Coverage} from 'collect-v8-coverage'; import * as fs from 'graceful-fs'; import jestMock = require('jest-mock'); import HasteMap = require('jest-haste-map'); -import Resolver = require('jest-resolve'); +import Resolver from 'jest-resolve'; import Snapshot = require('jest-snapshot'); import stripBOM = require('strip-bom'); import type {Context as JestContext} from './types'; diff --git a/packages/jest-runtime/src/types.ts b/packages/jest-runtime/src/types.ts index 7ef3887ec060..99f9ca0f9c08 100644 --- a/packages/jest-runtime/src/types.ts +++ b/packages/jest-runtime/src/types.ts @@ -6,12 +6,12 @@ */ import type {Config} from '@jest/types'; -import type {ResolverType} from 'jest-resolve'; +import type Resolver from 'jest-resolve'; import type {FS as HasteFS, ModuleMap} from 'jest-haste-map'; export type Context = { config: Config.ProjectConfig; hasteFS: HasteFS; moduleMap: ModuleMap; - resolver: ResolverType; + resolver: Resolver; };