Skip to content

Commit b8a9a71

Browse files
jeysalSimenB
authored andcommitted
chore: migrate jest-resolve-dependencies to TypeScript (#7922)
1 parent d490e2d commit b8a9a71

File tree

11 files changed

+146
-86
lines changed

11 files changed

+146
-86
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- `[@jest/core]` Create new package, which is `jest-cli` minus `yargs` and `prompts` ([#7696](https://github.com/facebook/jest/pull/7696))
4949
- `[@jest/transform]`: Migrate to TypeScript ([#7918](https://github.com/facebook/jest/pull/7918))
5050
- `[docs]` Add missing import to docs ([#7928](https://github.com/facebook/jest/pull/7928))
51+
- `[jest-resolve-dependencies]`: Migrate to TypeScript ([#7922](https://github.com/facebook/jest/pull/7922))
5152

5253
### Performance
5354

packages/jest-resolve-dependencies/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@
88
},
99
"license": "MIT",
1010
"main": "build/index.js",
11+
"types": "build/index.d.ts",
1112
"dependencies": {
13+
"@jest/types": "^24.1.0",
1214
"jest-regex-util": "^24.0.0",
1315
"jest-snapshot": "^24.1.0"
1416
},
17+
"devDependencies": {
18+
"jest-haste-map": "^24.0.0",
19+
"jest-resolve": "^24.1.0",
20+
"jest-runtime": "^24.1.0"
21+
},
22+
"peerDependencies": {
23+
"jest-haste-map": "^24.0.0",
24+
"jest-resolve": "^24.1.0"
25+
},
1526
"engines": {
1627
"node": ">= 6"
1728
},

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,44 @@
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-
*
76
*/
8-
'use strict';
97

10-
const path = require('path');
11-
const {normalize} = require('jest-config');
12-
const {buildSnapshotResolver} = require('jest-snapshot');
13-
const DependencyResolver = require('../index');
8+
import {tmpdir} from 'os';
9+
import path from 'path';
10+
import {Config} from '@jest/types';
11+
import {buildSnapshotResolver} from 'jest-snapshot';
12+
import {makeProjectConfig} from '../../../../TestUtils';
13+
14+
import DependencyResolver from '../index';
1415

1516
const maxWorkers = 1;
16-
let dependencyResolver;
17+
let dependencyResolver: DependencyResolver;
1718
let Runtime;
18-
let config;
19-
const cases = {
19+
let config: Config.ProjectConfig;
20+
const cases: {[key: string]: jest.Mock} = {
2021
fancyCondition: jest.fn(path => path.length > 10),
2122
testRegex: jest.fn(path => /.test.js$/.test(path)),
2223
};
23-
const filter = path => Object.keys(cases).every(key => cases[key](path));
24+
const filter = (path: Config.Path) =>
25+
Object.keys(cases).every(key => cases[key](path));
2426

2527
beforeEach(() => {
2628
Runtime = require('jest-runtime');
27-
config = normalize(
28-
{
29-
rootDir: '.',
30-
roots: ['./packages/jest-resolve-dependencies'],
31-
},
32-
{},
33-
).options;
34-
return Runtime.createContext(config, {maxWorkers}).then(hasteMap => {
35-
dependencyResolver = new DependencyResolver(
36-
hasteMap.resolver,
37-
hasteMap.hasteFS,
38-
buildSnapshotResolver(config),
39-
);
29+
config = makeProjectConfig({
30+
cacheDirectory: path.resolve(tmpdir(), 'jest-resolve-dependencies-test'),
31+
moduleDirectories: ['node_modules'],
32+
rootDir: '.',
33+
roots: ['./packages/jest-resolve-dependencies'],
4034
});
35+
return Runtime.createContext(config, {maxWorkers, watchman: false}).then(
36+
(hasteMap: any) => {
37+
dependencyResolver = new DependencyResolver(
38+
hasteMap.resolver,
39+
hasteMap.hasteFS,
40+
buildSnapshotResolver(config),
41+
);
42+
},
43+
);
4144
});
4245

4346
test('resolves no dependencies for non-existent path', () => {

packages/jest-resolve-dependencies/src/index.js renamed to packages/jest-resolve-dependencies/src/index.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,42 @@
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 {HasteFS} from 'types/HasteMap';
11-
import type {Path} from 'types/Config';
12-
import type {
13-
Resolver,
14-
ResolveModuleConfig,
15-
ResolvedModule,
16-
} from 'types/Resolve';
17-
import type {SnapshotResolver} from 'types/SnapshotResolver';
8+
import {Config, Resolve, Snapshot} from '@jest/types';
9+
import {FS as HasteFS} from 'jest-haste-map';
10+
import Resolver from 'jest-resolve';
1811
import {isSnapshotPath} from 'jest-snapshot';
1912

2013
/**
2114
* DependencyResolver is used to resolve the direct dependencies of a module or
2215
* to retrieve a list of all transitive inverse dependencies.
2316
*/
2417
class DependencyResolver {
25-
_hasteFS: HasteFS;
26-
_resolver: Resolver;
27-
_snapshotResolver: SnapshotResolver;
18+
private _hasteFS: HasteFS;
19+
private _resolver: Resolver;
20+
private _snapshotResolver: Snapshot.SnapshotResolver;
2821

2922
constructor(
3023
resolver: Resolver,
3124
hasteFS: HasteFS,
32-
snapshotResolver: SnapshotResolver,
25+
snapshotResolver: Snapshot.SnapshotResolver,
3326
) {
3427
this._resolver = resolver;
3528
this._hasteFS = hasteFS;
3629
this._snapshotResolver = snapshotResolver;
3730
}
3831

39-
resolve(file: Path, options?: ResolveModuleConfig): Array<Path> {
32+
resolve(
33+
file: Config.Path,
34+
options?: Resolve.ResolveModuleConfig,
35+
): Array<Config.Path> {
4036
const dependencies = this._hasteFS.getDependencies(file);
4137
if (!dependencies) {
4238
return [];
4339
}
4440

45-
return dependencies.reduce((acc, dependency) => {
41+
return dependencies.reduce<Array<Config.Path>>((acc, dependency) => {
4642
if (this._resolver.isCoreModule(dependency)) {
4743
return acc;
4844
}
@@ -66,23 +62,27 @@ class DependencyResolver {
6662
}
6763

6864
resolveInverseModuleMap(
69-
paths: Set<Path>,
70-
filter: (file: Path) => boolean,
71-
options?: ResolveModuleConfig,
72-
): Array<ResolvedModule> {
65+
paths: Set<Config.Path>,
66+
filter: (file: Config.Path) => boolean,
67+
options?: Resolve.ResolveModuleConfig,
68+
): Array<Resolve.ResolvedModule> {
7369
if (!paths.size) {
7470
return [];
7571
}
7672

77-
const collectModules = (related, moduleMap, changed) => {
73+
const collectModules = (
74+
related: Set<Config.Path>,
75+
moduleMap: Array<Resolve.ResolvedModule>,
76+
changed: Set<Config.Path>,
77+
) => {
7878
const visitedModules = new Set();
79-
const result: Array<ResolvedModule> = [];
79+
const result: Array<Resolve.ResolvedModule> = [];
8080
while (changed.size) {
8181
changed = new Set(
82-
moduleMap.reduce((acc, module) => {
82+
moduleMap.reduce<Array<Config.Path>>((acc, module) => {
8383
if (
8484
visitedModules.has(module.file) ||
85-
!module.dependencies.some(dep => dep && changed.has(dep))
85+
!module.dependencies.some(dep => changed.has(dep))
8686
) {
8787
return acc;
8888
}
@@ -98,11 +98,13 @@ class DependencyResolver {
9898
}, []),
9999
);
100100
}
101-
return result.concat(Array.from(related).map(file => ({file})));
101+
return result.concat(
102+
Array.from(related).map(file => ({dependencies: [], file})),
103+
);
102104
};
103105

104-
const relatedPaths = new Set<Path>();
105-
const changed = new Set();
106+
const relatedPaths = new Set<Config.Path>();
107+
const changed: Set<Config.Path> = new Set();
106108
for (const path of paths) {
107109
if (this._hasteFS.exists(path)) {
108110
const modulePath = isSnapshotPath(path)
@@ -114,7 +116,7 @@ class DependencyResolver {
114116
}
115117
}
116118
}
117-
const modules = [];
119+
const modules: Array<Resolve.ResolvedModule> = [];
118120
for (const file of this._hasteFS.getAbsoluteFileIterator()) {
119121
modules.push({
120122
dependencies: this.resolve(file, options),
@@ -125,14 +127,14 @@ class DependencyResolver {
125127
}
126128

127129
resolveInverse(
128-
paths: Set<Path>,
129-
filter: (file: Path) => boolean,
130-
options?: ResolveModuleConfig,
131-
): Array<Path> {
130+
paths: Set<Config.Path>,
131+
filter: (file: Config.Path) => boolean,
132+
options?: Resolve.ResolveModuleConfig,
133+
): Array<Config.Path> {
132134
return this.resolveInverseModuleMap(paths, filter, options).map(
133135
module => module.file,
134136
);
135137
}
136138
}
137139

138-
module.exports = DependencyResolver;
140+
export = DependencyResolver;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build"
6+
},
7+
"references": [
8+
{"path": "../jest-regex-util"},
9+
{"path": "../jest-snapshot"},
10+
{"path": "../jest-types"}
11+
]
12+
}

packages/jest-resolve/src/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import path from 'path';
9-
import {Config} from '@jest/types';
9+
import {Config, Resolve} from '@jest/types';
1010
import {ModuleMap} from 'jest-haste-map';
1111
import {sync as realpath} from 'realpath-native';
1212
import chalk from 'chalk';
@@ -15,11 +15,6 @@ import isBuiltinModule from './isBuiltinModule';
1515
import defaultResolver from './defaultResolver';
1616
import {ResolverConfig} from './types';
1717

18-
type ResolveModuleConfig = {
19-
skipNodeResolution?: boolean;
20-
paths?: Config.Path[];
21-
};
22-
2318
type FindNodeModuleConfig = {
2419
basedir: Config.Path;
2520
browser?: boolean;
@@ -102,7 +97,7 @@ class Resolver {
10297
resolveModuleFromDirIfExists(
10398
dirname: Config.Path,
10499
moduleName: string,
105-
options?: ResolveModuleConfig,
100+
options?: Resolve.ResolveModuleConfig,
106101
): Config.Path | null {
107102
const paths = (options && options.paths) || this._options.modulePaths;
108103
const moduleDirectory = this._options.moduleDirectories;
@@ -185,7 +180,7 @@ class Resolver {
185180
resolveModule(
186181
from: Config.Path,
187182
moduleName: string,
188-
options?: ResolveModuleConfig,
183+
options?: Resolve.ResolveModuleConfig,
189184
): Config.Path {
190185
const dirname = path.dirname(from);
191186
const module = this.resolveModuleFromDirIfExists(

packages/jest-snapshot/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
*/
77

88
import fs from 'fs';
9-
import {Config, Matchers} from '@jest/types';
9+
import {Config, Matchers, Snapshot} from '@jest/types';
1010
import {FS as HasteFS} from 'jest-haste-map';
1111

1212
import diff from 'jest-diff';
1313
import {EXPECTED_COLOR, matcherHint, RECEIVED_COLOR} from 'jest-matcher-utils';
1414
import {
1515
buildSnapshotResolver,
1616
isSnapshotPath,
17-
SnapshotResolver,
1817
EXTENSION,
1918
} from './snapshot_resolver';
2019
import SnapshotState from './State';
@@ -31,7 +30,7 @@ const fileExists = (filePath: Config.Path, hasteFS: HasteFS): boolean =>
3130
const cleanup = (
3231
hasteFS: HasteFS,
3332
update: Config.SnapshotUpdateState,
34-
snapshotResolver: SnapshotResolver,
33+
snapshotResolver: Snapshot.SnapshotResolver,
3534
) => {
3635
const pattern = '\\.' + EXTENSION + '$';
3736
const files = hasteFS.matchFiles(pattern);

packages/jest-snapshot/src/snapshot_resolver.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,19 @@
66
*/
77

88
import path from 'path';
9-
import {Config} from '@jest/types';
9+
import {Config, Snapshot} from '@jest/types';
1010
import chalk from 'chalk';
1111

12-
export type SnapshotResolver = {
13-
testPathForConsistencyCheck: string;
14-
resolveSnapshotPath(testPath: Config.Path, extension?: string): Config.Path;
15-
resolveTestPath(snapshotPath: Config.Path, extension?: string): Config.Path;
16-
};
17-
1812
export const EXTENSION = 'snap';
1913
export const DOT_EXTENSION = '.' + EXTENSION;
2014

2115
export const isSnapshotPath = (path: string): boolean =>
2216
path.endsWith(DOT_EXTENSION);
2317

24-
const cache: Map<Config.Path, SnapshotResolver> = new Map();
18+
const cache: Map<Config.Path, Snapshot.SnapshotResolver> = new Map();
2519
export const buildSnapshotResolver = (
2620
config: Config.ProjectConfig,
27-
): SnapshotResolver => {
21+
): Snapshot.SnapshotResolver => {
2822
const key = config.rootDir;
2923
if (!cache.has(key)) {
3024
cache.set(key, createSnapshotResolver(config.snapshotResolver));
@@ -34,13 +28,13 @@ export const buildSnapshotResolver = (
3428

3529
function createSnapshotResolver(
3630
snapshotResolverPath?: Config.Path | null,
37-
): SnapshotResolver {
31+
): Snapshot.SnapshotResolver {
3832
return typeof snapshotResolverPath === 'string'
3933
? createCustomSnapshotResolver(snapshotResolverPath)
4034
: createDefaultSnapshotResolver();
4135
}
4236

43-
function createDefaultSnapshotResolver(): SnapshotResolver {
37+
function createDefaultSnapshotResolver(): Snapshot.SnapshotResolver {
4438
return {
4539
resolveSnapshotPath: (testPath: Config.Path) =>
4640
path.join(
@@ -65,10 +59,10 @@ function createDefaultSnapshotResolver(): SnapshotResolver {
6559

6660
function createCustomSnapshotResolver(
6761
snapshotResolverPath: Config.Path,
68-
): SnapshotResolver {
69-
const custom: SnapshotResolver = require(snapshotResolverPath);
62+
): Snapshot.SnapshotResolver {
63+
const custom: Snapshot.SnapshotResolver = require(snapshotResolverPath);
7064

71-
const keys: [keyof SnapshotResolver, string][] = [
65+
const keys: [keyof Snapshot.SnapshotResolver, string][] = [
7266
['resolveSnapshotPath', 'function'],
7367
['resolveTestPath', 'function'],
7468
['testPathForConsistencyCheck', 'string'],
@@ -101,7 +95,7 @@ function mustImplement(propName: string, requiredType: string) {
10195
);
10296
}
10397

104-
function verifyConsistentTransformations(custom: SnapshotResolver) {
98+
function verifyConsistentTransformations(custom: Snapshot.SnapshotResolver) {
10599
const resolvedSnapshotPath = custom.resolveSnapshotPath(
106100
custom.testPathForConsistencyCheck,
107101
);

0 commit comments

Comments
 (0)