diff --git a/CHANGELOG.md b/CHANGELOG.md index a760de897316..4e77fabe32bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ - `[jest-haste-map]`: Migrate to TypeScript ([#7854](https://github.com/facebook/jest/pull/7854)) - `[docs]`: Fix image paths in SnapshotTesting.md for current and version 24 ([#7872](https://github.com/facebook/jest/pull/7872)) - `[babel-jest]`: Migrate to TypeScript ([#7862](https://github.com/facebook/jest/pull/7862)) +- `[jest-resolve]`: Migrate to TypeScript ([#7871](https://github.com/facebook/jest/pull/7871)) ### Performance diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.js.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.js.snap index 312abe793d4a..687ca938186f 100644 --- a/e2e/__tests__/__snapshots__/moduleNameMapper.test.js.snap +++ b/e2e/__tests__/__snapshots__/moduleNameMapper.test.js.snap @@ -30,6 +30,6 @@ FAIL __tests__/index.js 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:436:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:434:17) at Object.require (index.js:10:1) `; diff --git a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.js.snap b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.js.snap index 407b4c3bac23..13dc33fad1c2 100644 --- a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.js.snap +++ b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.js.snap @@ -33,6 +33,6 @@ FAIL __tests__/test.js | ^ 4 | - at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:203:17) + at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:201:17) at Object.require (index.js:3:18) `; diff --git a/package.json b/package.json index 08349c89df70..9767523ea0a2 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "progress": "^2.0.0", "promise": "^8.0.2", "readable-stream": "^3.0.3", - "realpath-native": "^1.0.0", + "realpath-native": "^1.1.0", "resolve": "^1.4.0", "rimraf": "^2.6.2", "slash": "^2.0.0", diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index 90c8e788397b..bc0caf236cad 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -35,7 +35,7 @@ "p-each-series": "^1.0.0", "pirates": "^4.0.0", "prompts": "^2.0.1", - "realpath-native": "^1.0.0", + "realpath-native": "^1.1.0", "rimraf": "^2.5.4", "slash": "^2.0.0", "string-length": "^2.0.0", diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index c1dc8cde0f46..fb97cfa3117a 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -23,7 +23,7 @@ "jest-validate": "^24.0.0", "micromatch": "^3.1.10", "pretty-format": "^24.0.0", - "realpath-native": "^1.0.2" + "realpath-native": "^1.1.0" }, "devDependencies": { "@types/babel__core": "^7.0.4", diff --git a/packages/jest-haste-map/src/ModuleMap.ts b/packages/jest-haste-map/src/ModuleMap.ts index 07246b1babcb..8b86aa2845e5 100644 --- a/packages/jest-haste-map/src/ModuleMap.ts +++ b/packages/jest-haste-map/src/ModuleMap.ts @@ -41,9 +41,9 @@ export default class ModuleMap { getModule( name: string, - platform: string | null, - supportsNativePlatform: boolean | null, - type: HTypeValue | null, + platform?: string | null, + supportsNativePlatform?: boolean | null, + type?: HTypeValue | null, ): Config.Path | null { if (type == null) { type = H.MODULE; @@ -62,7 +62,7 @@ export default class ModuleMap { getPackage( name: string, - platform: string | null, + platform: string | null | undefined, _supportsNativePlatform: boolean | null, ): Config.Path | null { return this.getModule(name, platform, null, H.PACKAGE); @@ -111,7 +111,7 @@ export default class ModuleMap { */ private _getModuleMetadata( name: string, - platform: string | null, + platform: string | null | undefined, supportsNativePlatform: boolean, ): ModuleMetaData | null { const map = this._raw.map.get(name) || EMPTY_OBJ; diff --git a/packages/jest-resolve/package.json b/packages/jest-resolve/package.json index d44ad9c6b183..07bcc5de9bb7 100644 --- a/packages/jest-resolve/package.json +++ b/packages/jest-resolve/package.json @@ -8,15 +8,20 @@ }, "license": "MIT", "main": "build/index.js", + "types": "build/index.d.ts", "dependencies": { + "@jest/types": "^24.1.0", "browser-resolve": "^1.11.3", "chalk": "^2.0.1", - "realpath-native": "^1.0.0" + "realpath-native": "^1.1.0" }, "devDependencies": { "@types/browser-resolve": "^0.0.5", "jest-haste-map": "^24.0.0" }, + "peerDependencies": { + "jest-haste-map": "^24.0.0" + }, "engines": { "node": ">= 6" }, diff --git a/packages/jest-resolve/src/__tests__/isBuiltinModule.test.js b/packages/jest-resolve/src/__tests__/isBuiltinModule.test.ts similarity index 98% rename from packages/jest-resolve/src/__tests__/isBuiltinModule.test.js rename to packages/jest-resolve/src/__tests__/isBuiltinModule.test.ts index ba68e4fa1b9e..0bbbb84d732b 100644 --- a/packages/jest-resolve/src/__tests__/isBuiltinModule.test.js +++ b/packages/jest-resolve/src/__tests__/isBuiltinModule.test.ts @@ -1,5 +1,4 @@ // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. -// @flow import isBuiltinModule from '../isBuiltinModule'; diff --git a/packages/jest-resolve/src/__tests__/resolve.test.js b/packages/jest-resolve/src/__tests__/resolve.test.ts similarity index 92% rename from packages/jest-resolve/src/__tests__/resolve.test.js rename to packages/jest-resolve/src/__tests__/resolve.test.ts index d074456ca2ad..014407bc47c7 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.js +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -6,16 +6,18 @@ * */ -'use strict'; - import fs from 'fs'; import path from 'path'; -import {ModuleMap} from 'jest-haste-map'; -// eslint-disable-next-line import/default +import HasteMap from 'jest-haste-map'; import Resolver from '../'; +// @ts-ignore: js file import userResolver from '../__mocks__/userResolver'; import nodeModulesPaths from '../nodeModulesPaths'; import defaultResolver from '../defaultResolver'; +import {ResolverConfig} from '../types'; + +// @ts-ignore: types are wrong. not sure how... +const {ModuleMap} = HasteMap; jest.mock('../__mocks__/userResolver'); @@ -28,21 +30,21 @@ describe('isCoreModule', () => { const moduleMap = ModuleMap.create('/'); const resolver = new Resolver(moduleMap, { hasCoreModules: false, - }); + } as ResolverConfig); const isCore = resolver.isCoreModule('assert'); expect(isCore).toEqual(false); }); it('returns true if `hasCoreModules` is true and `moduleName` is a core module.', () => { const moduleMap = ModuleMap.create('/'); - const resolver = new Resolver(moduleMap, {}); + const resolver = new Resolver(moduleMap, {} as ResolverConfig); const isCore = resolver.isCoreModule('assert'); expect(isCore).toEqual(true); }); it('returns false if `hasCoreModules` is true and `moduleName` is not a core module.', () => { const moduleMap = ModuleMap.create('/'); - const resolver = new Resolver(moduleMap, {}); + const resolver = new Resolver(moduleMap, {} as ResolverConfig); const isCore = resolver.isCoreModule('not-a-core-module'); expect(isCore).toEqual(false); }); @@ -84,7 +86,7 @@ describe('findNodeModule', () => { }); describe('resolveModule', () => { - let moduleMap; + let moduleMap: typeof ModuleMap; beforeEach(() => { moduleMap = ModuleMap.create('/'); }); @@ -92,7 +94,7 @@ describe('resolveModule', () => { it('is possible to resolve node modules', () => { const resolver = new Resolver(moduleMap, { extensions: ['.js'], - }); + } as ResolverConfig); const src = require.resolve('../'); const resolved = resolver.resolveModule( src, @@ -104,7 +106,7 @@ describe('resolveModule', () => { it('is possible to resolve node modules with custom extensions', () => { const resolver = new Resolver(moduleMap, { extensions: ['.js', '.jsx'], - }); + } as ResolverConfig); const src = require.resolve('../'); const resolvedJsx = resolver.resolveModule( src, @@ -119,7 +121,7 @@ describe('resolveModule', () => { const resolver = new Resolver(moduleMap, { extensions: ['.js', '.jsx'], platforms: ['native'], - }); + } as ResolverConfig); const src = require.resolve('../'); const resolvedJsx = resolver.resolveModule( src, @@ -133,7 +135,7 @@ describe('resolveModule', () => { it('is possible to resolve node modules by resolving their realpath', () => { const resolver = new Resolver(moduleMap, { extensions: ['.js'], - }); + } as ResolverConfig); const src = path.join( path.resolve(__dirname, '../../src/__mocks__/bar/node_modules/'), 'foo/index.js', @@ -147,7 +149,7 @@ describe('resolveModule', () => { it('is possible to specify custom resolve paths', () => { const resolver = new Resolver(moduleMap, { extensions: ['.js'], - }); + } as ResolverConfig); const src = require.resolve('../'); const resolved = resolver.resolveModule(src, 'mockJsDependency', { paths: [ @@ -173,7 +175,7 @@ describe('getMockModule', () => { }, ], resolver: require.resolve('../__mocks__/userResolver'), - }); + } as ResolverConfig); const src = require.resolve('../'); resolver.getMockModule(src, 'dependentModule'); @@ -196,7 +198,7 @@ describe('nodeModulesPaths', () => { describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => { const _path = path; - let moduleMap; + let moduleMap: typeof ModuleMap; beforeEach(() => { jest.resetModules(); @@ -208,7 +210,7 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => { // This test suite won't work otherwise, since we cannot make assumptions // about the test environment when it comes to absolute paths. jest.doMock('realpath-native', () => ({ - sync: dirInput => dirInput, + sync: (dirInput: string) => dirInput, })); }); diff --git a/packages/jest-resolve/src/defaultResolver.js b/packages/jest-resolve/src/defaultResolver.ts similarity index 80% rename from packages/jest-resolve/src/defaultResolver.js rename to packages/jest-resolve/src/defaultResolver.ts index 3549bf4d1933..6cda0840d98e 100644 --- a/packages/jest-resolve/src/defaultResolver.js +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -3,33 +3,29 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ -import type {Path} from 'types/Config'; -import type {ErrorWithCode} from 'types/Errors'; - -import browserResolve from 'browser-resolve'; import fs from 'fs'; import path from 'path'; +import browserResolve from 'browser-resolve'; +import {Config} from '@jest/types'; import isBuiltinModule from './isBuiltinModule'; import nodeModulesPaths from './nodeModulesPaths'; -type ResolverOptions = {| - basedir: Path, - browser?: boolean, - defaultResolver: typeof defaultResolver, - extensions?: Array, - moduleDirectory?: Array, - paths?: ?Array, - rootDir: ?Path, -|}; +type ResolverOptions = { + basedir: Config.Path; + browser?: boolean; + defaultResolver: typeof defaultResolver; + extensions?: Array; + moduleDirectory?: Array; + paths?: Array; + rootDir?: Config.Path; +}; export default function defaultResolver( - path: Path, + path: Config.Path, options: ResolverOptions, -): Path { +): Config.Path { const resolve = options.browser ? browserResolve.sync : resolveSync; return resolve(path, { @@ -44,7 +40,10 @@ export default function defaultResolver( const REGEX_RELATIVE_IMPORT = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/; -function resolveSync(target: Path, options: ResolverOptions): Path { +function resolveSync( + target: Config.Path, + options: ResolverOptions, +): Config.Path { const basedir = options.basedir; const extensions = options.extensions || ['.js']; const paths = options.paths || []; @@ -75,7 +74,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path { return target; } - const err: ErrorWithCode = new Error( + const err: Error & {code?: string} = new Error( "Cannot find module '" + target + "' from '" + basedir + "'", ); err.code = 'MODULE_NOT_FOUND'; @@ -84,7 +83,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path { /* * contextual helper functions */ - function tryResolve(name: Path): ?Path { + function tryResolve(name: Config.Path): Config.Path | undefined { const dir = path.dirname(name); let result; if (isDirectory(dir)) { @@ -98,7 +97,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path { return result; } - function resolveAsFile(name: Path): ?Path { + function resolveAsFile(name: Config.Path): Config.Path | undefined { if (isFile(name)) { return name; } @@ -113,7 +112,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path { return undefined; } - function resolveAsDirectory(name: Path): ?Path { + function resolveAsDirectory(name: Config.Path): Config.Path | undefined { if (!isDirectory(name)) { return undefined; } @@ -140,7 +139,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path { /* * helper functions */ -function isFile(file: Path): boolean { +function isFile(file: Config.Path): boolean { let result; try { @@ -156,7 +155,7 @@ function isFile(file: Path): boolean { return result; } -function isDirectory(dir: Path): boolean { +function isDirectory(dir: Config.Path): boolean { let result; try { @@ -172,6 +171,6 @@ function isDirectory(dir: Path): boolean { return result; } -function isCurrentDirectory(testPath: Path): boolean { +function isCurrentDirectory(testPath: Config.Path): boolean { return path.resolve('.') === path.resolve(testPath); } diff --git a/packages/jest-resolve/src/index.js b/packages/jest-resolve/src/index.ts similarity index 83% rename from packages/jest-resolve/src/index.js rename to packages/jest-resolve/src/index.ts index b75fb2b6afd0..cfc053f08001 100644 --- a/packages/jest-resolve/src/index.js +++ b/packages/jest-resolve/src/index.ts @@ -3,51 +3,34 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ -import type {Path} from 'types/Config'; -import type {ModuleMap} from 'types/HasteMap'; -import type {ResolveModuleConfig} from 'types/Resolve'; -import type {ErrorWithCode} from 'types/Errors'; - import path from 'path'; +import {Config} from '@jest/types'; +import {ModuleMap} from 'jest-haste-map'; import {sync as realpath} from 'realpath-native'; +import chalk from 'chalk'; import nodeModulesPaths from './nodeModulesPaths'; import isBuiltinModule from './isBuiltinModule'; import defaultResolver from './defaultResolver'; -import chalk from 'chalk'; +import {ResolverConfig} from './types'; -type ResolverConfig = {| - browser?: boolean, - defaultPlatform: ?string, - extensions: Array, - hasCoreModules: boolean, - moduleDirectories: Array, - moduleNameMapper: ?Array, - modulePaths: Array, - platforms?: Array, - resolver: ?Path, - rootDir: ?Path, -|}; - -type FindNodeModuleConfig = {| - basedir: Path, - browser?: boolean, - extensions?: Array, - moduleDirectory?: Array, - paths?: Array, - resolver?: ?Path, - rootDir?: ?Path, -|}; - -type ModuleNameMapperConfig = {| - regex: RegExp, - moduleName: string, -|}; +type ResolveModuleConfig = { + skipNodeResolution?: boolean; + paths?: Config.Path[]; +}; -type BooleanObject = {[key: string]: boolean, __proto__: null}; +type FindNodeModuleConfig = { + basedir: Config.Path; + browser?: boolean; + extensions?: Array; + moduleDirectory?: Array; + paths?: Array; + resolver?: Config.Path; + rootDir?: Config.Path; +}; + +type BooleanObject = {[key: string]: boolean}; const NATIVE_PLATFORM = 'native'; @@ -62,12 +45,12 @@ const nodePaths = process.env.NODE_PATH : null; class Resolver { - _options: ResolverConfig; - _moduleMap: ModuleMap; - _moduleIDCache: {[key: string]: string, __proto__: null}; - _moduleNameCache: {[name: string]: Path, __proto__: null}; - _modulePathCache: {[path: Path]: Array, __proto__: null}; - _supportsNativePlatform: boolean; + private readonly _options: ResolverConfig; + private readonly _moduleMap: ModuleMap; + private readonly _moduleIDCache: {[key: string]: string}; + private readonly _moduleNameCache: {[name: string]: Config.Path}; + private readonly _modulePathCache: {[path: string]: Array}; + private readonly _supportsNativePlatform: boolean; constructor(moduleMap: ModuleMap, options: ResolverConfig) { this._options = { @@ -92,7 +75,10 @@ class Resolver { this._modulePathCache = Object.create(null); } - static findNodeModule(path: Path, options: FindNodeModuleConfig): ?Path { + static findNodeModule( + path: Config.Path, + options: FindNodeModuleConfig, + ): Config.Path | null { const resolver = options.resolver ? /* $FlowFixMe */ require(options.resolver) @@ -114,10 +100,10 @@ class Resolver { } resolveModuleFromDirIfExists( - dirname: Path, + dirname: Config.Path, moduleName: string, options?: ResolveModuleConfig, - ): ?Path { + ): Config.Path | null { const paths = (options && options.paths) || this._options.modulePaths; const moduleDirectory = this._options.moduleDirectories; const key = dirname + path.delimiter + moduleName; @@ -157,7 +143,7 @@ class Resolver { const skipResolution = options && options.skipNodeResolution && !moduleName.includes(path.sep); - const resolveNodeModule = name => + const resolveNodeModule = (name: Config.Path) => Resolver.findNodeModule(name, { basedir: dirname, browser: this._options.browser, @@ -179,7 +165,7 @@ class Resolver { // 4. Resolve "haste packages" which are `package.json` files outside of // `node_modules` folders anywhere in the file system. const parts = moduleName.split('/'); - const hastePackage = this.getPackage(parts.shift()); + const hastePackage = this.getPackage(parts.shift()!); if (hastePackage) { try { const module = path.join.apply( @@ -197,10 +183,10 @@ class Resolver { } resolveModule( - from: Path, + from: Config.Path, moduleName: string, options?: ResolveModuleConfig, - ): Path { + ): Config.Path { const dirname = path.dirname(from); const module = this.resolveModuleFromDirIfExists( dirname, @@ -213,10 +199,10 @@ class Resolver { // produces an error based on the dirname but we have the actual current // module name available. const relativePath = path.relative(dirname, from); - const err = new Error( + const err: Error & {code?: string} = new Error( `Cannot find module '${moduleName}' from '${relativePath || '.'}'`, ); - (err: ErrorWithCode).code = 'MODULE_NOT_FOUND'; + err.code = 'MODULE_NOT_FOUND'; throw err; } @@ -224,7 +210,7 @@ class Resolver { return this._options.hasCoreModules && isBuiltinModule(moduleName); } - getModule(name: string): ?Path { + getModule(name: string): Config.Path | null { return this._moduleMap.getModule( name, this._options.defaultPlatform, @@ -232,14 +218,14 @@ class Resolver { ); } - getModulePath(from: Path, moduleName: string) { + getModulePath(from: Config.Path, moduleName: string) { if (moduleName[0] !== '.' || path.isAbsolute(moduleName)) { return moduleName; } return path.normalize(path.dirname(from) + '/' + moduleName); } - getPackage(name: string): ?Path { + getPackage(name: string): Config.Path | null { return this._moduleMap.getPackage( name, this._options.defaultPlatform, @@ -247,7 +233,7 @@ class Resolver { ); } - getMockModule(from: Path, name: string): ?Path { + getMockModule(from: Config.Path, name: string): Config.Path | null { const mock = this._moduleMap.getMockModule(name); if (mock) { return mock; @@ -260,7 +246,7 @@ class Resolver { return null; } - getModulePaths(from: Path): Array { + getModulePaths(from: Config.Path): Array { if (!this._modulePathCache[from]) { const moduleDirectory = this._options.moduleDirectories; const paths = nodeModulesPaths(from, {moduleDirectory}); @@ -275,8 +261,8 @@ class Resolver { getModuleID( virtualMocks: BooleanObject, - from: Path, - _moduleName?: ?string, + from: Config.Path, + _moduleName?: string, ): string { const moduleName = _moduleName || ''; @@ -299,15 +285,15 @@ class Resolver { return (this._moduleIDCache[key] = id); } - _getModuleType(moduleName: string): 'node' | 'user' { + private _getModuleType(moduleName: string): 'node' | 'user' { return this.isCoreModule(moduleName) ? 'node' : 'user'; } - _getAbsolutePath( + private _getAbsolutePath( virtualMocks: BooleanObject, - from: Path, + from: Config.Path, moduleName: string, - ): ?string { + ): Config.Path | null { if (this.isCoreModule(moduleName)) { return moduleName; } @@ -316,17 +302,20 @@ class Resolver { : this._getVirtualMockPath(virtualMocks, from, moduleName); } - _getMockPath(from: Path, moduleName: string): ?string { + private _getMockPath( + from: Config.Path, + moduleName: string, + ): Config.Path | null { return !this.isCoreModule(moduleName) ? this.getMockModule(from, moduleName) : null; } - _getVirtualMockPath( + private _getVirtualMockPath( virtualMocks: BooleanObject, - from: Path, + from: Config.Path, moduleName: string, - ): Path { + ): Config.Path { const virtualMockPath = this.getModulePath(from, moduleName); return virtualMocks[virtualMockPath] ? virtualMockPath @@ -335,13 +324,16 @@ class Resolver { : from; } - _isModuleResolved(from: Path, moduleName: string): boolean { + private _isModuleResolved(from: Config.Path, moduleName: string): boolean { return !!( this.getModule(moduleName) || this.getMockModule(from, moduleName) ); } - resolveStubModuleName(from: Path, moduleName: string): ?Path { + resolveStubModuleName( + from: Config.Path, + moduleName: string, + ): Config.Path | null { const dirname = path.dirname(from); const paths = this._options.modulePaths; const extensions = this._options.extensions.slice(); @@ -404,11 +396,11 @@ class Resolver { } const createNoMappedModuleFoundError = ( - moduleName, - updatedName, - mappedModuleName, - regex, - resolver, + moduleName: string, + updatedName: string, + mappedModuleName: string, + regex: RegExp, + resolver: Function | string, ) => { const error = new Error( chalk.red(`${chalk.bold('Configuration error')}: @@ -430,4 +422,4 @@ Please check your configuration for these entries: return error; }; -module.exports = Resolver; +export = Resolver; diff --git a/packages/jest-resolve/src/isBuiltinModule.js b/packages/jest-resolve/src/isBuiltinModule.ts similarity index 67% rename from packages/jest-resolve/src/isBuiltinModule.js rename to packages/jest-resolve/src/isBuiltinModule.ts index bd674f65864e..c7e64c914434 100644 --- a/packages/jest-resolve/src/isBuiltinModule.js +++ b/packages/jest-resolve/src/isBuiltinModule.ts @@ -3,23 +3,20 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ -// $FlowFixMe: Flow doesn't know about the `module` module -import {builtinModules} from 'module'; +import module from 'module'; -// https://github.com/facebook/flow/pull/5160 -declare var process: { - binding(type: string): {}, +// @ts-ignore: "private" api +declare const process: { + binding(type: string): {}; }; const EXPERIMENTAL_MODULES = ['worker_threads']; const BUILTIN_MODULES = new Set( - builtinModules - ? builtinModules.concat(EXPERIMENTAL_MODULES) + module.builtinModules + ? module.builtinModules.concat(EXPERIMENTAL_MODULES) : Object.keys(process.binding('natives')) .filter((module: string) => !/^internal\//.test(module)) .concat(EXPERIMENTAL_MODULES), diff --git a/packages/jest-resolve/src/nodeModulesPaths.js b/packages/jest-resolve/src/nodeModulesPaths.ts similarity index 84% rename from packages/jest-resolve/src/nodeModulesPaths.js rename to packages/jest-resolve/src/nodeModulesPaths.ts index 3e714f9d2b3d..4526660808d6 100644 --- a/packages/jest-resolve/src/nodeModulesPaths.js +++ b/packages/jest-resolve/src/nodeModulesPaths.ts @@ -5,26 +5,24 @@ * LICENSE file in the root directory of this source tree. * * Adapted from: https://github.com/substack/node-resolve - * - * @flow */ -import type {Path} from 'types/Config'; import path from 'path'; +import {Config} from '@jest/types'; import {sync as realpath} from 'realpath-native'; -type NodeModulesPathsOptions = {| - moduleDirectory?: Array, - paths?: ?Array, -|}; +type NodeModulesPathsOptions = { + moduleDirectory?: Array; + paths?: Array; +}; export default function nodeModulesPaths( - basedir: Path, + basedir: Config.Path, options: NodeModulesPathsOptions, -): Path[] { +): Config.Path[] { const modules = options && options.moduleDirectory - ? [].concat(options.moduleDirectory) + ? Array.from(options.moduleDirectory) : ['node_modules']; // ensure that `basedir` is an absolute path at this point, @@ -48,7 +46,7 @@ export default function nodeModulesPaths( physicalBasedir = basedirAbs; } - const paths = [physicalBasedir]; + const paths: Array = [physicalBasedir]; let parsed = path.parse(physicalBasedir); while (parsed.dir !== paths[paths.length - 1]) { paths.push(parsed.dir); @@ -67,7 +65,7 @@ export default function nodeModulesPaths( : path.join(prefix, aPath, moduleDir), ), ), - [], + [] as Array, ) .filter(dir => dir !== ''); diff --git a/packages/jest-resolve/src/types.ts b/packages/jest-resolve/src/types.ts new file mode 100644 index 000000000000..b765f0333107 --- /dev/null +++ b/packages/jest-resolve/src/types.ts @@ -0,0 +1,26 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {Config} from '@jest/types'; + +export type ResolverConfig = { + browser?: boolean; + defaultPlatform?: string; + extensions: Array; + hasCoreModules: boolean; + moduleDirectories: Array; + moduleNameMapper?: Array; + modulePaths: Array; + platforms?: Array; + resolver: Config.Path; + rootDir: Config.Path; +}; + +type ModuleNameMapperConfig = { + regex: RegExp; + moduleName: string; +}; diff --git a/packages/jest-resolve/tsconfig.json b/packages/jest-resolve/tsconfig.json new file mode 100644 index 000000000000..befa0d307f51 --- /dev/null +++ b/packages/jest-resolve/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "references": [{"path": "../jest-types"}, {"path": "../jest-haste-map"}] +} diff --git a/packages/jest-runtime/package.json b/packages/jest-runtime/package.json index 2bfa2989ad6d..8524b6900536 100644 --- a/packages/jest-runtime/package.json +++ b/packages/jest-runtime/package.json @@ -26,7 +26,7 @@ "jest-util": "^24.0.0", "jest-validate": "^24.0.0", "micromatch": "^3.1.10", - "realpath-native": "^1.0.0", + "realpath-native": "^1.1.0", "slash": "^2.0.0", "strip-bom": "^3.0.0", "write-file-atomic": "2.4.1", diff --git a/yarn.lock b/yarn.lock index 4d6dea0f8c63..111752599f80 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11063,10 +11063,10 @@ readdirp@^2.0.0: micromatch "^3.1.10" readable-stream "^2.0.2" -realpath-native@^1.0.0, realpath-native@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/realpath-native/-/realpath-native-1.0.2.tgz#cd51ce089b513b45cf9b1516c82989b51ccc6560" - integrity sha512-+S3zTvVt9yTntFrBpm7TQmQ3tzpCrnA1a/y+3cUHAc9ZR6aIjG0WNLR+Rj79QpJktY+VeW/TQtFlQ1bzsehI8g== +realpath-native@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/realpath-native/-/realpath-native-1.1.0.tgz#2003294fea23fb0672f2476ebe22fcf498a2d65c" + integrity sha512-wlgPA6cCIIg9gKz0fgAPjnzh4yR/LnXovwuo9hvyGvx3h8nX4+/iLZplfUWasXpqD8BdnGnP5njOFjkUwPzvjA== dependencies: util.promisify "^1.0.0"