Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7d70b2f

Browse files
committedFeb 13, 2019
chore: migrate jest-resolve to TypeScript
1 parent 300de98 commit 7d70b2f

18 files changed

+180
-153
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- `[jest-haste-map]`: Migrate to TypeScript ([#7854](https://github.com/facebook/jest/pull/7854))
3232
- `[docs]`: Fix image paths in SnapshotTesting.md for current and version 24 ([#7872](https://github.com/facebook/jest/pull/7872))
3333
- `[babel-jest]`: Migrate to TypeScript ([#7862](https://github.com/facebook/jest/pull/7862))
34+
- `[jest-resolve]`: Migrate to TypeScript ([#7871](https://github.com/facebook/jest/pull/7871))
3435

3536
### Performance
3637

‎e2e/__tests__/__snapshots__/moduleNameMapper.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ FAIL __tests__/index.js
3030
12 | module.exports = () => 'test';
3131
13 |
3232
33-
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:436:17)
33+
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:434:17)
3434
at Object.require (index.js:10:1)
3535
`;

‎e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ FAIL __tests__/test.js
3333
| ^
3434
4 |
3535
36-
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:203:17)
36+
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:201:17)
3737
at Object.require (index.js:3:18)
3838
`;

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"progress": "^2.0.0",
6363
"promise": "^8.0.2",
6464
"readable-stream": "^3.0.3",
65-
"realpath-native": "^1.0.0",
65+
"realpath-native": "^1.1.0",
6666
"resolve": "^1.4.0",
6767
"rimraf": "^2.6.2",
6868
"slash": "^2.0.0",

‎packages/jest-cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"p-each-series": "^1.0.0",
3636
"pirates": "^4.0.0",
3737
"prompts": "^2.0.1",
38-
"realpath-native": "^1.0.0",
38+
"realpath-native": "^1.1.0",
3939
"rimraf": "^2.5.4",
4040
"slash": "^2.0.0",
4141
"string-length": "^2.0.0",

‎packages/jest-config/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"jest-validate": "^24.0.0",
2424
"micromatch": "^3.1.10",
2525
"pretty-format": "^24.0.0",
26-
"realpath-native": "^1.0.2"
26+
"realpath-native": "^1.1.0"
2727
},
2828
"devDependencies": {
2929
"@types/babel__core": "^7.0.4",

‎packages/jest-haste-map/src/ModuleMap.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export default class ModuleMap {
4141

4242
getModule(
4343
name: string,
44-
platform: string | null,
45-
supportsNativePlatform: boolean | null,
46-
type: HTypeValue | null,
44+
platform?: string | null,
45+
supportsNativePlatform?: boolean | null,
46+
type?: HTypeValue | null,
4747
): Config.Path | null {
4848
if (type == null) {
4949
type = H.MODULE;
@@ -62,7 +62,7 @@ export default class ModuleMap {
6262

6363
getPackage(
6464
name: string,
65-
platform: string | null,
65+
platform: string | null | undefined,
6666
_supportsNativePlatform: boolean | null,
6767
): Config.Path | null {
6868
return this.getModule(name, platform, null, H.PACKAGE);
@@ -111,7 +111,7 @@ export default class ModuleMap {
111111
*/
112112
private _getModuleMetadata(
113113
name: string,
114-
platform: string | null,
114+
platform: string | null | undefined,
115115
supportsNativePlatform: boolean,
116116
): ModuleMetaData | null {
117117
const map = this._raw.map.get(name) || EMPTY_OBJ;

‎packages/jest-resolve/package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
},
99
"license": "MIT",
1010
"main": "build/index.js",
11+
"types": "build/index.d.ts",
1112
"dependencies": {
13+
"@jest/types": "^24.1.0",
1214
"browser-resolve": "^1.11.3",
1315
"chalk": "^2.0.1",
14-
"realpath-native": "^1.0.0"
16+
"realpath-native": "^1.1.0"
1517
},
1618
"devDependencies": {
1719
"@types/browser-resolve": "^0.0.5",
1820
"jest-haste-map": "^24.0.0"
1921
},
22+
"peerDependencies": {
23+
"jest-haste-map": "^24.0.0"
24+
},
2025
"engines": {
2126
"node": ">= 6"
2227
},

‎packages/jest-resolve/src/__tests__/isBuiltinModule.test.js renamed to ‎packages/jest-resolve/src/__tests__/isBuiltinModule.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2-
// @flow
32

43
import isBuiltinModule from '../isBuiltinModule';
54

‎packages/jest-resolve/src/__tests__/resolve.test.js renamed to ‎packages/jest-resolve/src/__tests__/resolve.test.ts

+18-16
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
*
77
*/
88

9-
'use strict';
10-
119
import fs from 'fs';
1210
import path from 'path';
13-
import {ModuleMap} from 'jest-haste-map';
14-
// eslint-disable-next-line import/default
11+
import HasteMap from 'jest-haste-map';
1512
import Resolver from '../';
13+
// @ts-ignore: js file
1614
import userResolver from '../__mocks__/userResolver';
1715
import nodeModulesPaths from '../nodeModulesPaths';
1816
import defaultResolver from '../defaultResolver';
17+
import {ResolverConfig} from '../types';
18+
19+
// @ts-ignore: types are wrong. not sure how...
20+
const {ModuleMap} = HasteMap;
1921

2022
jest.mock('../__mocks__/userResolver');
2123

@@ -28,21 +30,21 @@ describe('isCoreModule', () => {
2830
const moduleMap = ModuleMap.create('/');
2931
const resolver = new Resolver(moduleMap, {
3032
hasCoreModules: false,
31-
});
33+
} as ResolverConfig);
3234
const isCore = resolver.isCoreModule('assert');
3335
expect(isCore).toEqual(false);
3436
});
3537

3638
it('returns true if `hasCoreModules` is true and `moduleName` is a core module.', () => {
3739
const moduleMap = ModuleMap.create('/');
38-
const resolver = new Resolver(moduleMap, {});
40+
const resolver = new Resolver(moduleMap, {} as ResolverConfig);
3941
const isCore = resolver.isCoreModule('assert');
4042
expect(isCore).toEqual(true);
4143
});
4244

4345
it('returns false if `hasCoreModules` is true and `moduleName` is not a core module.', () => {
4446
const moduleMap = ModuleMap.create('/');
45-
const resolver = new Resolver(moduleMap, {});
47+
const resolver = new Resolver(moduleMap, {} as ResolverConfig);
4648
const isCore = resolver.isCoreModule('not-a-core-module');
4749
expect(isCore).toEqual(false);
4850
});
@@ -84,15 +86,15 @@ describe('findNodeModule', () => {
8486
});
8587

8688
describe('resolveModule', () => {
87-
let moduleMap;
89+
let moduleMap: typeof ModuleMap;
8890
beforeEach(() => {
8991
moduleMap = ModuleMap.create('/');
9092
});
9193

9294
it('is possible to resolve node modules', () => {
9395
const resolver = new Resolver(moduleMap, {
9496
extensions: ['.js'],
95-
});
97+
} as ResolverConfig);
9698
const src = require.resolve('../');
9799
const resolved = resolver.resolveModule(
98100
src,
@@ -104,7 +106,7 @@ describe('resolveModule', () => {
104106
it('is possible to resolve node modules with custom extensions', () => {
105107
const resolver = new Resolver(moduleMap, {
106108
extensions: ['.js', '.jsx'],
107-
});
109+
} as ResolverConfig);
108110
const src = require.resolve('../');
109111
const resolvedJsx = resolver.resolveModule(
110112
src,
@@ -119,7 +121,7 @@ describe('resolveModule', () => {
119121
const resolver = new Resolver(moduleMap, {
120122
extensions: ['.js', '.jsx'],
121123
platforms: ['native'],
122-
});
124+
} as ResolverConfig);
123125
const src = require.resolve('../');
124126
const resolvedJsx = resolver.resolveModule(
125127
src,
@@ -133,7 +135,7 @@ describe('resolveModule', () => {
133135
it('is possible to resolve node modules by resolving their realpath', () => {
134136
const resolver = new Resolver(moduleMap, {
135137
extensions: ['.js'],
136-
});
138+
} as ResolverConfig);
137139
const src = path.join(
138140
path.resolve(__dirname, '../../src/__mocks__/bar/node_modules/'),
139141
'foo/index.js',
@@ -147,7 +149,7 @@ describe('resolveModule', () => {
147149
it('is possible to specify custom resolve paths', () => {
148150
const resolver = new Resolver(moduleMap, {
149151
extensions: ['.js'],
150-
});
152+
} as ResolverConfig);
151153
const src = require.resolve('../');
152154
const resolved = resolver.resolveModule(src, 'mockJsDependency', {
153155
paths: [
@@ -173,7 +175,7 @@ describe('getMockModule', () => {
173175
},
174176
],
175177
resolver: require.resolve('../__mocks__/userResolver'),
176-
});
178+
} as ResolverConfig);
177179
const src = require.resolve('../');
178180
resolver.getMockModule(src, 'dependentModule');
179181

@@ -196,7 +198,7 @@ describe('nodeModulesPaths', () => {
196198

197199
describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
198200
const _path = path;
199-
let moduleMap;
201+
let moduleMap: typeof ModuleMap;
200202

201203
beforeEach(() => {
202204
jest.resetModules();
@@ -208,7 +210,7 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
208210
// This test suite won't work otherwise, since we cannot make assumptions
209211
// about the test environment when it comes to absolute paths.
210212
jest.doMock('realpath-native', () => ({
211-
sync: dirInput => dirInput,
213+
sync: (dirInput: string) => dirInput,
212214
}));
213215
});
214216

‎packages/jest-resolve/src/defaultResolver.js renamed to ‎packages/jest-resolve/src/defaultResolver.ts

+24-25
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,29 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
import type {Path} from 'types/Config';
11-
import type {ErrorWithCode} from 'types/Errors';
12-
13-
import browserResolve from 'browser-resolve';
148
import fs from 'fs';
159
import path from 'path';
10+
import browserResolve from 'browser-resolve';
11+
import {Config} from '@jest/types';
1612
import isBuiltinModule from './isBuiltinModule';
1713
import nodeModulesPaths from './nodeModulesPaths';
1814

19-
type ResolverOptions = {|
20-
basedir: Path,
21-
browser?: boolean,
22-
defaultResolver: typeof defaultResolver,
23-
extensions?: Array<string>,
24-
moduleDirectory?: Array<string>,
25-
paths?: ?Array<Path>,
26-
rootDir: ?Path,
27-
|};
15+
type ResolverOptions = {
16+
basedir: Config.Path;
17+
browser?: boolean;
18+
defaultResolver: typeof defaultResolver;
19+
extensions?: Array<string>;
20+
moduleDirectory?: Array<string>;
21+
paths?: Array<Config.Path>;
22+
rootDir?: Config.Path;
23+
};
2824

2925
export default function defaultResolver(
30-
path: Path,
26+
path: Config.Path,
3127
options: ResolverOptions,
32-
): Path {
28+
): Config.Path {
3329
const resolve = options.browser ? browserResolve.sync : resolveSync;
3430

3531
return resolve(path, {
@@ -44,7 +40,10 @@ export default function defaultResolver(
4440

4541
const REGEX_RELATIVE_IMPORT = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/;
4642

47-
function resolveSync(target: Path, options: ResolverOptions): Path {
43+
function resolveSync(
44+
target: Config.Path,
45+
options: ResolverOptions,
46+
): Config.Path {
4847
const basedir = options.basedir;
4948
const extensions = options.extensions || ['.js'];
5049
const paths = options.paths || [];
@@ -75,7 +74,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
7574
return target;
7675
}
7776

78-
const err: ErrorWithCode = new Error(
77+
const err: Error & {code?: string} = new Error(
7978
"Cannot find module '" + target + "' from '" + basedir + "'",
8079
);
8180
err.code = 'MODULE_NOT_FOUND';
@@ -84,7 +83,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
8483
/*
8584
* contextual helper functions
8685
*/
87-
function tryResolve(name: Path): ?Path {
86+
function tryResolve(name: Config.Path): Config.Path | undefined {
8887
const dir = path.dirname(name);
8988
let result;
9089
if (isDirectory(dir)) {
@@ -98,7 +97,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
9897
return result;
9998
}
10099

101-
function resolveAsFile(name: Path): ?Path {
100+
function resolveAsFile(name: Config.Path): Config.Path | undefined {
102101
if (isFile(name)) {
103102
return name;
104103
}
@@ -113,7 +112,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
113112
return undefined;
114113
}
115114

116-
function resolveAsDirectory(name: Path): ?Path {
115+
function resolveAsDirectory(name: Config.Path): Config.Path | undefined {
117116
if (!isDirectory(name)) {
118117
return undefined;
119118
}
@@ -140,7 +139,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
140139
/*
141140
* helper functions
142141
*/
143-
function isFile(file: Path): boolean {
142+
function isFile(file: Config.Path): boolean {
144143
let result;
145144

146145
try {
@@ -156,7 +155,7 @@ function isFile(file: Path): boolean {
156155
return result;
157156
}
158157

159-
function isDirectory(dir: Path): boolean {
158+
function isDirectory(dir: Config.Path): boolean {
160159
let result;
161160

162161
try {
@@ -172,6 +171,6 @@ function isDirectory(dir: Path): boolean {
172171
return result;
173172
}
174173

175-
function isCurrentDirectory(testPath: Path): boolean {
174+
function isCurrentDirectory(testPath: Config.Path): boolean {
176175
return path.resolve('.') === path.resolve(testPath);
177176
}

0 commit comments

Comments
 (0)
Please sign in to comment.