diff --git a/packages/jest-resolve/src/ModuleNotFoundError.ts b/packages/jest-resolve/src/ModuleNotFoundError.ts index a2b0935c6348..7b6685c5ba96 100644 --- a/packages/jest-resolve/src/ModuleNotFoundError.ts +++ b/packages/jest-resolve/src/ModuleNotFoundError.ts @@ -7,12 +7,11 @@ import * as path from 'path'; import slash = require('slash'); -import type {Config} from '@jest/types'; export default class ModuleNotFoundError extends Error { public code = 'MODULE_NOT_FOUND'; public hint?: string; - public requireStack?: Array; + public requireStack?: Array; public siblingWithSimilarExtensionFound?: boolean; public moduleName?: string; @@ -24,7 +23,7 @@ export default class ModuleNotFoundError extends Error { this.moduleName = moduleName; } - public buildMessage(rootDir: Config.Path): void { + public buildMessage(rootDir: string): void { if (!this._originalMessage) { this._originalMessage = this.message || ''; } diff --git a/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/package.json b/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/package.json index 7fd4bab2a661..89b8152b8398 100644 --- a/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/package.json +++ b/packages/jest-resolve/src/__mocks__/conditions/node_modules/exports/package.json @@ -8,7 +8,7 @@ "default": "./default.js" }, "./nested": "./nestedDefault.js", - "./deeplyNested" : { + "./deeplyNested": { "require": "./nestedRequire.js", "default": "./nestedDefault.js" } diff --git a/packages/jest-resolve/src/__mocks__/package.json b/packages/jest-resolve/src/__mocks__/package.json index 2807645e6dca..16d6367d52d4 100644 --- a/packages/jest-resolve/src/__mocks__/package.json +++ b/packages/jest-resolve/src/__mocks__/package.json @@ -1,6 +1,5 @@ { "name": "__mocks__", "version": "1.0.0", - "dependencies": { - } + "dependencies": {} } diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 411d52e9dd86..19598ec43e81 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -13,7 +13,6 @@ import { resolve as resolveExports, } from 'resolve.exports'; import slash = require('slash'); -import type {Config} from '@jest/types'; import { PkgJson, isDirectoryAsync, @@ -30,14 +29,14 @@ const resolveSync = resolveAsync.sync; // copy from `resolve`'s types so we don't have their types in our definition // files export interface ResolverOptions { - basedir: Config.Path; + basedir: string; browser?: boolean; conditions?: Array; defaultResolver: typeof defaultResolver; extensions?: Array; moduleDirectory?: Array; - paths?: Array; - rootDir?: Config.Path; + paths?: Array; + rootDir?: string; packageFilter?: (pkg: PkgJson, dir: string) => PkgJson; pathFilter?: (pkg: PkgJson, path: string, relativePath: string) => string; } @@ -56,9 +55,9 @@ declare global { } export function defaultResolver( - path: Config.Path, + path: string, options: ResolverOptions, -): Config.Path { +): string { // Yarn 2 adds support to `resolve` automatically so the pnpResolver is only // needed for Yarn 1 which implements version 1 of the pnp spec if (process.versions.pnp === '1') { @@ -73,9 +72,9 @@ export function defaultResolver( } export async function defaultResolverAsync( - path: Config.Path, + path: string, options: ResolverOptionsAsync, -): Promise { +): Promise { // Yarn 2 adds support to `resolve` automatically so the pnpResolver is only // needed for Yarn 1 which implements version 1 of the pnp spec if (process.versions.pnp === '1') { @@ -101,7 +100,7 @@ export async function defaultResolverAsync( * getSyncResolveOptions returns resolution options that are used synchronously. */ function getSyncResolveOptions( - path: Config.Path, + path: string, options: ResolverOptions, ): resolveAsync.SyncOpts { return { @@ -120,7 +119,7 @@ function getSyncResolveOptions( * getAsyncResolveOptions returns resolution options that are used asynchronously. */ function getAsyncResolveOptions( - path: Config.Path, + path: string, options: ResolverOptionsAsync, ): resolveAsync.AsyncOpts { return { @@ -139,13 +138,13 @@ function getAsyncResolveOptions( * helper functions */ -function readPackageSync(_: unknown, file: Config.Path): PkgJson { +function readPackageSync(_: unknown, file: string): PkgJson { return readPackageCached(file); } function readPackageAsync( _: unknown, - pkgfile: Config.Path, + pkgfile: string, cb: (err: Error | null, pkgJson?: PkgJson) => void, ): void { try { @@ -158,7 +157,7 @@ function readPackageAsync( } function createPackageFilter( - originalPath: Config.Path, + originalPath: string, userFilter?: ResolverOptions['packageFilter'], ): ResolverOptions['packageFilter'] { if (shouldIgnoreRequestForExports(originalPath)) { @@ -186,7 +185,7 @@ function createPackageFilter( } function createPathFilter( - originalPath: Config.Path, + originalPath: string, conditions?: Array, userFilter?: ResolverOptions['pathFilter'], ): ResolverOptions['pathFilter'] { @@ -221,5 +220,5 @@ function createPathFilter( } // if it's a relative import or an absolute path, exports are ignored -const shouldIgnoreRequestForExports = (path: Config.Path) => +const shouldIgnoreRequestForExports = (path: string) => path.startsWith('.') || isAbsolute(path); diff --git a/packages/jest-resolve/src/fileWalkers.ts b/packages/jest-resolve/src/fileWalkers.ts index ea2cffa78fdd..1112e213fc6c 100644 --- a/packages/jest-resolve/src/fileWalkers.ts +++ b/packages/jest-resolve/src/fileWalkers.ts @@ -7,7 +7,6 @@ import {dirname, resolve} from 'path'; import * as fs from 'graceful-fs'; -import type {Config} from '@jest/types'; import {tryRealpath} from 'jest-util'; export function clearFsCache(): void { @@ -53,7 +52,7 @@ function statSyncCached(path: string): IPathType { } const checkedRealpathPaths = new Map(); -function realpathCached(path: Config.Path): Config.Path { +function realpathCached(path: string): string { let result = checkedRealpathPaths.get(path); if (result != null) { @@ -75,7 +74,7 @@ function realpathCached(path: Config.Path): Config.Path { export type PkgJson = Record; const packageContents = new Map(); -export function readPackageCached(path: Config.Path): PkgJson { +export function readPackageCached(path: string): PkgJson { let result = packageContents.get(path); if (result != null) { @@ -92,9 +91,7 @@ export function readPackageCached(path: Config.Path): PkgJson { // adapted from // https://github.com/lukeed/escalade/blob/2477005062cdbd8407afc90d3f48f4930354252b/src/sync.js // to use cached `fs` calls -export function findClosestPackageJson( - start: Config.Path, -): Config.Path | undefined { +export function findClosestPackageJson(start: string): string | undefined { let dir = resolve('.', start); if (!isDirectorySync(dir)) { dir = dirname(dir); @@ -120,12 +117,12 @@ export function findClosestPackageJson( /* * helper functions */ -export function isFileSync(file: Config.Path): boolean { +export function isFileSync(file: string): boolean { return statSyncCached(file) === IPathType.FILE; } export function isFileAsync( - file: Config.Path, + file: string, cb: (err: Error | null, isFile?: boolean) => void, ): void { try { @@ -137,12 +134,12 @@ export function isFileAsync( } } -export function isDirectorySync(dir: Config.Path): boolean { +export function isDirectorySync(dir: string): boolean { return statSyncCached(dir) === IPathType.DIRECTORY; } export function isDirectoryAsync( - dir: Config.Path, + dir: string, cb: (err: Error | null, isDir?: boolean) => void, ): void { try { @@ -154,13 +151,13 @@ export function isDirectoryAsync( } } -export function realpathSync(file: Config.Path): Config.Path { +export function realpathSync(file: string): string { return realpathCached(file); } export function realpathAsync( - file: Config.Path, - cb: (err: Error | null, resolved?: Config.Path) => void, + file: string, + cb: (err: Error | null, resolved?: string) => void, ): void { try { // TODO: create an async version of realpathCached diff --git a/packages/jest-resolve/src/nodeModulesPaths.ts b/packages/jest-resolve/src/nodeModulesPaths.ts index 604e071c01fe..a2e91914de66 100644 --- a/packages/jest-resolve/src/nodeModulesPaths.ts +++ b/packages/jest-resolve/src/nodeModulesPaths.ts @@ -8,18 +8,17 @@ */ import * as path from 'path'; -import type {Config} from '@jest/types'; import {tryRealpath} from 'jest-util'; type NodeModulesPathsOptions = { moduleDirectory?: Array; - paths?: Array; + paths?: Array; }; export default function nodeModulesPaths( - basedir: Config.Path, + basedir: string, options: NodeModulesPathsOptions, -): Array { +): Array { const modules = options && options.moduleDirectory ? Array.from(options.moduleDirectory) @@ -46,7 +45,7 @@ export default function nodeModulesPaths( physicalBasedir = basedirAbs; } - const paths: Array = [physicalBasedir]; + const paths: Array = [physicalBasedir]; let parsed = path.parse(physicalBasedir); while (parsed.dir !== paths[paths.length - 1]) { paths.push(parsed.dir); @@ -54,7 +53,7 @@ export default function nodeModulesPaths( } const dirs = paths - .reduce>( + .reduce>( (dirs, aPath) => dirs.concat( modules.map(moduleDir => diff --git a/packages/jest-resolve/src/resolver.ts b/packages/jest-resolve/src/resolver.ts index 3fb04003e2f6..b70f319ea37f 100644 --- a/packages/jest-resolve/src/resolver.ts +++ b/packages/jest-resolve/src/resolver.ts @@ -10,7 +10,6 @@ import * as path from 'path'; import chalk = require('chalk'); import slash = require('slash'); -import type {Config} from '@jest/types'; import type {IModuleMap} from 'jest-haste-map'; import {tryRealpath} from 'jest-util'; import ModuleNotFoundError from './ModuleNotFoundError'; @@ -22,21 +21,21 @@ import shouldLoadAsEsm, {clearCachedLookups} from './shouldLoadAsEsm'; import type {ResolverConfig} from './types'; type FindNodeModuleConfig = { - basedir: Config.Path; + basedir: string; browser?: boolean; conditions?: Array; extensions?: Array; moduleDirectory?: Array; - paths?: Array; - resolver?: Config.Path | null; - rootDir?: Config.Path; + paths?: Array; + resolver?: string | null; + rootDir?: string; throwIfNotFound?: boolean; }; export type ResolveModuleConfig = { conditions?: Array; skipNodeResolution?: boolean; - paths?: Array; + paths?: Array; }; const NATIVE_PLATFORM = 'native'; @@ -55,8 +54,8 @@ export default class Resolver { private readonly _options: ResolverConfig; private readonly _moduleMap: IModuleMap; private readonly _moduleIDCache: Map; - private readonly _moduleNameCache: Map; - private readonly _modulePathCache: Map>; + private readonly _moduleNameCache: Map; + private readonly _modulePathCache: Map>; private readonly _supportsNativePlatform: boolean; constructor(moduleMap: IModuleMap, options: ResolverConfig) { @@ -107,9 +106,9 @@ export default class Resolver { static unstable_shouldLoadAsEsm = shouldLoadAsEsm; static findNodeModule( - path: Config.Path, + path: string, options: FindNodeModuleConfig, - ): Config.Path | null { + ): string | null { // The resolver module could be a synchronous function, or an object with sync and/or async keys. const resolverModule = options.resolver ? require(options.resolver) : null; let resolver = defaultResolver; @@ -145,9 +144,9 @@ export default class Resolver { } static async findNodeModuleAsync( - path: Config.Path, + path: string, options: FindNodeModuleConfig, - ): Promise { + ): Promise { // The resolver module could be a synchronous function, or an object with sync and/or async keys. const resolverModule = options.resolver ? require(options.resolver) : null; const resolver: typeof defaultResolverAsync = @@ -184,7 +183,7 @@ export default class Resolver { * methods, to try to keep them as DRY as possible. */ private _prepareForResolution( - dirname: Config.Path, + dirname: string, moduleName: string, options?: ResolveModuleConfig, ) { @@ -224,7 +223,7 @@ export default class Resolver { return null; } - private _throwModNotFoundError(from: Config.Path, moduleName: string): never { + private _throwModNotFoundError(from: string, moduleName: string): never { const relativePath = slash(path.relative(this._options.rootDir, from)) || '.'; @@ -259,10 +258,10 @@ export default class Resolver { private _getAbsolutePath( virtualMocks: Map, - from: Config.Path, + from: string, moduleName: string, options?: ResolveModuleConfig, - ): Config.Path | null { + ): string | null { if (this.isCoreModule(moduleName)) { return moduleName; } @@ -273,10 +272,10 @@ export default class Resolver { private async _getAbsolutePathAsync( virtualMocks: Map, - from: Config.Path, + from: string, moduleName: string, options?: ResolveModuleConfig, - ): Promise { + ): Promise { if (this.isCoreModule(moduleName)) { return moduleName; } @@ -294,19 +293,16 @@ export default class Resolver { ); } - private _getMockPath( - from: Config.Path, - moduleName: string, - ): Config.Path | null { + private _getMockPath(from: string, moduleName: string): string | null { return !this.isCoreModule(moduleName) ? this.getMockModule(from, moduleName) : null; } private async _getMockPathAsync( - from: Config.Path, + from: string, moduleName: string, - ): Promise { + ): Promise { return !this.isCoreModule(moduleName) ? await this.getMockModuleAsync(from, moduleName) : null; @@ -314,10 +310,10 @@ export default class Resolver { private _getVirtualMockPath( virtualMocks: Map, - from: Config.Path, + from: string, moduleName: string, options?: ResolveModuleConfig, - ): Config.Path { + ): string { const virtualMockPath = this.getModulePath(from, moduleName); return virtualMocks.get(virtualMockPath) ? virtualMockPath @@ -328,10 +324,10 @@ export default class Resolver { private async _getVirtualMockPathAsync( virtualMocks: Map, - from: Config.Path, + from: string, moduleName: string, options?: ResolveModuleConfig, - ): Promise { + ): Promise { const virtualMockPath = this.getModulePath(from, moduleName); return virtualMocks.get(virtualMockPath) ? virtualMockPath @@ -340,14 +336,14 @@ export default class Resolver { : from; } - private _isModuleResolved(from: Config.Path, moduleName: string): boolean { + private _isModuleResolved(from: string, moduleName: string): boolean { return !!( this.getModule(moduleName) || this.getMockModule(from, moduleName) ); } private async _isModuleResolvedAsync( - from: Config.Path, + from: string, moduleName: string, ): Promise { return !!( @@ -356,7 +352,7 @@ export default class Resolver { ); } - private _setResolver(resolver?: Config.Path | null): void { + private _setResolver(resolver?: string | null): void { this._options.resolver = resolver; } @@ -370,7 +366,7 @@ export default class Resolver { ); } - getModule(name: string): Config.Path | null { + getModule(name: string): string | null { return this._moduleMap.getModule( name, this._options.defaultPlatform, @@ -378,14 +374,14 @@ export default class Resolver { ); } - getModulePath(from: Config.Path, moduleName: string): Config.Path { + getModulePath(from: string, moduleName: string): string { if (moduleName[0] !== '.' || path.isAbsolute(moduleName)) { return moduleName; } return path.normalize(path.dirname(from) + '/' + moduleName); } - getPackage(name: string): Config.Path | null { + getPackage(name: string): string | null { return this._moduleMap.getPackage( name, this._options.defaultPlatform, @@ -393,7 +389,7 @@ export default class Resolver { ); } - getModulePaths(from: Config.Path): Array { + getModulePaths(from: string): Array { const cachedModule = this._modulePathCache.get(from); if (cachedModule) { return cachedModule; @@ -409,10 +405,7 @@ export default class Resolver { return paths; } - resolveStubModuleName( - from: Config.Path, - moduleName: string, - ): Config.Path | null { + resolveStubModuleName(from: string, moduleName: string): string | null { const dirname = path.dirname(from); const {extensions, moduleDirectory, paths} = this._prepareForResolution( @@ -469,9 +462,9 @@ export default class Resolver { } async resolveStubModuleNameAsync( - from: Config.Path, + from: string, moduleName: string, - ): Promise { + ): Promise { const dirname = path.dirname(from); const {extensions, moduleDirectory, paths} = this._prepareForResolution( @@ -527,7 +520,7 @@ export default class Resolver { return null; } - getMockModule(from: Config.Path, name: string): Config.Path | null { + getMockModule(from: string, name: string): string | null { const mock = this._moduleMap.getMockModule(name); if (mock) { return mock; @@ -540,10 +533,7 @@ export default class Resolver { return null; } - async getMockModuleAsync( - from: Config.Path, - name: string, - ): Promise { + async getMockModuleAsync(from: string, name: string): Promise { const mock = this._moduleMap.getMockModule(name); if (mock) { return mock; @@ -558,7 +548,7 @@ export default class Resolver { getModuleID( virtualMocks: Map, - from: Config.Path, + from: string, _moduleName?: string, options?: ResolveModuleConfig, ): string { @@ -594,7 +584,7 @@ export default class Resolver { async getModuleIDAsync( virtualMocks: Map, - from: Config.Path, + from: string, moduleName = '', options?: ResolveModuleConfig, ): Promise { @@ -629,10 +619,10 @@ export default class Resolver { } resolveModuleFromDirIfExists( - dirname: Config.Path, + dirname: string, moduleName: string, options?: ResolveModuleConfig, - ): Config.Path | null { + ): string | null { const {extensions, key, moduleDirectory, paths, skipResolution} = this._prepareForResolution(dirname, moduleName, options); @@ -658,7 +648,7 @@ export default class Resolver { // requires). This enables us to speed up resolution when we build a // dependency graph because we don't have to look at modules that may not // exist and aren't mocked. - const resolveNodeModule = (name: Config.Path, throwIfNotFound = false) => { + const resolveNodeModule = (name: string, throwIfNotFound = false) => { if (this.isCoreModule(name)) { return name; } @@ -703,10 +693,10 @@ export default class Resolver { } async resolveModuleFromDirIfExistsAsync( - dirname: Config.Path, + dirname: string, moduleName: string, options?: ResolveModuleConfig, - ): Promise { + ): Promise { const {extensions, key, moduleDirectory, paths, skipResolution} = this._prepareForResolution(dirname, moduleName, options); @@ -732,10 +722,7 @@ export default class Resolver { // requires). This enables us to speed up resolution when we build a // dependency graph because we don't have to look at modules that may not // exist and aren't mocked. - const resolveNodeModule = async ( - name: Config.Path, - throwIfNotFound = false, - ) => { + const resolveNodeModule = async (name: string, throwIfNotFound = false) => { if (this.isCoreModule(name)) { return name; } @@ -784,10 +771,10 @@ export default class Resolver { } resolveModule( - from: Config.Path, + from: string, moduleName: string, options?: ResolveModuleConfig, - ): Config.Path { + ): string { const dirname = path.dirname(from); const module = this.resolveStubModuleName(from, moduleName) || @@ -801,10 +788,10 @@ export default class Resolver { } async resolveModuleAsync( - from: Config.Path, + from: string, moduleName: string, options?: ResolveModuleConfig, - ): Promise { + ): Promise { const dirname = path.dirname(from); const module = (await this.resolveStubModuleNameAsync(from, moduleName)) || diff --git a/packages/jest-resolve/src/shouldLoadAsEsm.ts b/packages/jest-resolve/src/shouldLoadAsEsm.ts index aadca23c4d01..f937913a95d5 100644 --- a/packages/jest-resolve/src/shouldLoadAsEsm.ts +++ b/packages/jest-resolve/src/shouldLoadAsEsm.ts @@ -8,7 +8,6 @@ import {dirname, extname} from 'path'; // @ts-expect-error: experimental, not added to the types import {SyntheticModule} from 'vm'; -import type {Config} from '@jest/types'; import {findClosestPackageJson, readPackageCached} from './fileWalkers'; const runtimeSupportsVmModules = typeof SyntheticModule === 'function'; @@ -24,8 +23,8 @@ export function clearCachedLookups(): void { } export default function cachedShouldLoadAsEsm( - path: Config.Path, - extensionsToTreatAsEsm: Array, + path: string, + extensionsToTreatAsEsm: Array, ): boolean { if (!runtimeSupportsVmModules) { return false; @@ -43,8 +42,8 @@ export default function cachedShouldLoadAsEsm( // this is a bad version of what https://github.com/nodejs/modules/issues/393 would provide function shouldLoadAsEsm( - path: Config.Path, - extensionsToTreatAsEsm: Array, + path: string, + extensionsToTreatAsEsm: Array, ): boolean { const extension = extname(path); @@ -72,7 +71,7 @@ function shouldLoadAsEsm( return cachedLookup; } -function cachedPkgCheck(cwd: Config.Path): boolean { +function cachedPkgCheck(cwd: string): boolean { const pkgPath = findClosestPackageJson(cwd); if (!pkgPath) { return false; diff --git a/packages/jest-resolve/src/types.ts b/packages/jest-resolve/src/types.ts index e2c672925fe4..363e1668389d 100644 --- a/packages/jest-resolve/src/types.ts +++ b/packages/jest-resolve/src/types.ts @@ -5,19 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -import type {Config} from '@jest/types'; - export type ResolverConfig = { defaultPlatform?: string | null; extensions: Array; hasCoreModules: boolean; moduleDirectories: Array; moduleNameMapper?: Array | null; - modulePaths?: Array; + modulePaths?: Array; platforms?: Array; - resolver?: Config.Path | null; - asyncResolver?: Config.Path | null; - rootDir: Config.Path; + resolver?: string | null; + asyncResolver?: string | null; + rootDir: string; }; type ModuleNameMapperConfig = { diff --git a/packages/jest-resolve/src/utils.ts b/packages/jest-resolve/src/utils.ts index fbbdf19598b8..6a51c4061800 100644 --- a/packages/jest-resolve/src/utils.ts +++ b/packages/jest-resolve/src/utils.ts @@ -7,7 +7,6 @@ import * as path from 'path'; import chalk = require('chalk'); -import type {Config} from '@jest/types'; import {ValidationError} from 'jest-validate'; import Resolver from './resolver'; @@ -19,10 +18,7 @@ const DOCUMENTATION_NOTE = ` ${chalk.bold('Configuration Documentation:')} const createValidationError = (message: string) => new ValidationError(`${BULLET}Validation Error`, message, DOCUMENTATION_NOTE); -const replaceRootDirInPath = ( - rootDir: Config.Path, - filePath: Config.Path, -): string => { +const replaceRootDirInPath = (rootDir: string, filePath: string): string => { if (!/^/.test(filePath)) { return filePath; } @@ -48,7 +44,7 @@ const resolveWithPrefix = ( optionName: string; prefix: string; requireResolveFunction: (moduleName: string) => string; - rootDir: Config.Path; + rootDir: string; }, ): string => { const fileName = replaceRootDirInPath(rootDir, filePath); @@ -98,7 +94,7 @@ export const resolveTestEnvironment = ({ testEnvironment: filePath, requireResolveFunction, }: { - rootDir: Config.Path; + rootDir: string; testEnvironment: string; requireResolveFunction: (moduleName: string) => string; }): string => { @@ -137,7 +133,7 @@ export const resolveWatchPlugin = ( requireResolveFunction, }: { filePath: string; - rootDir: Config.Path; + rootDir: string; requireResolveFunction: (moduleName: string) => string; }, ): string => @@ -166,7 +162,7 @@ export const resolveRunner = ( requireResolveFunction, }: { filePath: string; - rootDir: Config.Path; + rootDir: string; requireResolveFunction: (moduleName: string) => string; }, ): string => @@ -187,7 +183,7 @@ export const resolveSequencer = ( requireResolveFunction, }: { filePath: string; - rootDir: Config.Path; + rootDir: string; requireResolveFunction: (moduleName: string) => string; }, ): string =>