Skip to content

Commit

Permalink
chore: migrate jest-resolve to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 23, 2020
1 parent 1535af7 commit 15b3bca
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 54 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Chore & Maintenance

- `[jest-resolve]` [**BREAKING**] Migrate to ESM ([#10688](https://github.com/facebook/jest/pull/10688))

### Performance

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

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

Expand All @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(() => {
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 {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';
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 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 = {
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 @@ -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,
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 {
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 = {
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 @@ -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';
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 @@ -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 {
Expand All @@ -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,
) {
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 {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';
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
17 changes: 7 additions & 10 deletions packages/jest-resolve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ type FindNodeModuleConfig = {
// TODO: replace with a Map in Jest 26
type BooleanObject = Record<string, boolean>;

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 @@ -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';
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 @@ -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,
Expand Down Expand Up @@ -58,7 +58,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

0 comments on commit 15b3bca

Please sign in to comment.