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-resolve to ESM #10688

Merged
merged 1 commit into from
Nov 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)
`;

Expand Down Expand Up @@ -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)
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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)
`;
33 changes: 16 additions & 17 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
});
Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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);
});

Expand All @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-reporters/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -25,7 +25,7 @@ export type Context = {
config: Config.ProjectConfig;
hasteFS: HasteFS;
moduleMap: ModuleMap;
resolver: ResolverType;
resolver: Resolver;
};

export type Test = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
6 changes: 3 additions & 3 deletions packages/jest-resolve-dependencies/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
) {
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-resolve/src/defaultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
19 changes: 8 additions & 11 deletions packages/jest-resolve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, boolean>;

declare namespace Resolver {
export type ResolveModuleConfig = {
skipNodeResolution?: boolean;
paths?: Array<Config.Path>;
};
export type ResolverType = Resolver;
}
export type ResolveModuleConfig = {
skipNodeResolution?: boolean;
paths?: Array<Config.Path>;
};

const NATIVE_PLATFORM = 'native';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -502,4 +499,4 @@ Please check your configuration for these entries:
return error;
};

export = Resolver;
export default Resolver;
6 changes: 3 additions & 3 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<RunTestInternalResult> {
Expand Down Expand Up @@ -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<TestResult> {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-runner/src/testWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -57,7 +57,7 @@ const formatError = (error: string | ErrorWithCode): SerializableError => {
};
};

const resolvers = new Map<string, ResolverType>();
const resolvers = new Map<string, Resolver>();
const getResolver = (config: Config.ProjectConfig) => {
const resolver = resolvers.get(config.name);
if (!resolver) {
Expand Down
Loading