Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c6f14b

Browse files
authored
feat(jest-resolve): expose PackageFilter, PathFilter and PackageJSON types (#12712)
1 parent a293b75 commit 3c6f14b

File tree

9 files changed

+97
-35
lines changed

9 files changed

+97
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
- `[jest-resolve, jest-runtime]` Add support for `data:` URI import and mock ([#12392](https://github.com/facebook/jest/pull/12392))
4747
- `[jest-resolve, jest-runtime]` Add support for async resolver ([#11540](https://github.com/facebook/jest/pull/11540))
4848
- `[jest-resolve]` [**BREAKING**] Remove `browser?: boolean` from resolver options, `conditions: ['browser']` should be used instead ([#12707](https://github.com/facebook/jest/pull/12707))
49-
- `[jest-resolve]` Expose `JestResolver`, `AsyncResolver` and `SyncResolver` types ([#12707](https://github.com/facebook/jest/pull/12707))
49+
- `[jest-resolve]` Expose `JestResolver`, `AsyncResolver`, `SyncResolver`, `PackageFilter`, `PathFilter` and `PackageJSON` types ([#12707](https://github.com/facebook/jest/pull/12707), ([#12712](https://github.com/facebook/jest/pull/12712))
5050
- `[jest-runner]` Allow `setupFiles` module to export an async function ([#12042](https://github.com/facebook/jest/pull/12042))
5151
- `[jest-runner]` Allow passing `testEnvironmentOptions` via docblocks ([#12470](https://github.com/facebook/jest/pull/12470))
5252
- `[jest-runner]` Exposing `CallbackTestRunner`, `EmittingTestRunner` abstract classes to help typing third party runners ([#12646](https://github.com/facebook/jest/pull/12646))

docs/Configuration.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,6 @@ This option allows the use of a custom resolver. This resolver must be a module
927927
The options object provided to resolvers has the shape:
928928

929929
```ts
930-
type PackageJson = Record<string, unknown>;
931-
932930
type ResolverOptions = {
933931
/** Directory to begin resolving from. */
934932
basedir: string;
@@ -943,9 +941,9 @@ type ResolverOptions = {
943941
/** List of `require.paths` to use if nothing is found in `node_modules`. */
944942
paths?: Array<string>;
945943
/** Allows transforming parsed `package.json` contents. */
946-
packageFilter?: (pkg: PackageJson, file: string, dir: string) => PackageJson;
944+
packageFilter?: (pkg: PackageJSON, file: string, dir: string) => PackageJSON;
947945
/** Allows transforms a path within a package. */
948-
pathFilter?: (pkg: PackageJson, path: string, relativePath: string) => string;
946+
pathFilter?: (pkg: PackageJSON, path: string, relativePath: string) => string;
949947
/** Current root directory. */
950948
rootDir?: string;
951949
};

packages/jest-resolve/__typetests__/resolver.test.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,41 @@
66
*/
77

88
import {expectAssignable, expectError, expectType} from 'tsd-lite';
9-
import type {AsyncResolver, JestResolver, SyncResolver} from 'jest-resolve';
10-
11-
type PackageJson = Record<string, unknown>;
12-
type PackageFilter = (
13-
pkg: PackageJson,
14-
file: string,
15-
dir: string,
16-
) => PackageJson;
17-
type PathFilter = (
18-
pkg: PackageJson,
19-
path: string,
20-
relativePath: string,
21-
) => string;
9+
import type {
10+
AsyncResolver,
11+
JestResolver,
12+
PackageFilter,
13+
PackageJSON,
14+
PathFilter,
15+
SyncResolver,
16+
} from 'jest-resolve';
17+
18+
// PackageJSON
19+
20+
expectAssignable<PackageJSON>({
21+
caption: 'test',
22+
count: 100,
23+
isTest: true,
24+
location: {name: 'test', start: [1, 2], valid: false, x: 10, y: 20},
25+
values: [0, 10, 20, {x: 1, y: 2}, true, 'test', ['a', 'b']],
26+
});
27+
28+
expectError<PackageJSON>({
29+
filter: () => {},
30+
});
31+
32+
// PackageFilter
33+
34+
const packageFilter = (pkg: PackageJSON, file: string, dir: string) => pkg;
35+
36+
expectAssignable<PackageFilter>(packageFilter);
37+
38+
// PathFilter
39+
40+
const pathFilter = (pkg: PackageJSON, path: string, relativePath: string) =>
41+
relativePath;
42+
43+
expectAssignable<PathFilter>(pathFilter);
2244

2345
// AsyncResolver
2446

packages/jest-resolve/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"devDependencies": {
3131
"@tsd/typescript": "~4.6.2",
3232
"@types/graceful-fs": "^4.1.3",
33-
"@types/resolve": "^1.20.0",
33+
"@types/resolve": "^1.20.2",
3434
"tsd-lite": "^0.5.1"
3535
},
3636
"engines": {

packages/jest-resolve/src/defaultResolver.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,43 @@ import {
1313
resolve as resolveExports,
1414
} from 'resolve.exports';
1515
import {
16-
PackageJson,
1716
findClosestPackageJson,
1817
isDirectory,
1918
isFile,
2019
readPackageCached,
2120
realpathSync,
2221
} from './fileWalkers';
22+
import type {PackageJSON} from './types';
23+
24+
/**
25+
* Allows transforming parsed `package.json` contents.
26+
*
27+
* @param pkg - Parsed `package.json` contents.
28+
* @param file - Path to `package.json` file.
29+
* @param dir - Directory that contains the `package.json`.
30+
*
31+
* @returns Transformed `package.json` contents.
32+
*/
33+
export type PackageFilter = (
34+
pkg: PackageJSON,
35+
file: string,
36+
dir: string,
37+
) => PackageJSON;
38+
39+
/**
40+
* Allows transforms a path within a package.
41+
*
42+
* @param pkg - Parsed `package.json` contents.
43+
* @param path - Path being resolved.
44+
* @param relativePath - Path relative from the `package.json` location.
45+
*
46+
* @returns Relative path that will be joined from the `package.json` location.
47+
*/
48+
export type PathFilter = (
49+
pkg: PackageJSON,
50+
path: string,
51+
relativePath: string,
52+
) => string;
2353

2454
type ResolverOptions = {
2555
/** Directory to begin resolving from. */
@@ -45,9 +75,9 @@ type ResolverOptions = {
4575
*/
4676
paths?: Array<string>;
4777
/** Allows transforming parsed `package.json` contents. */
48-
packageFilter?: (pkg: PackageJson, file: string, dir: string) => PackageJson;
78+
packageFilter?: PackageFilter;
4979
/** Allows transforms a path within a package. */
50-
pathFilter?: (pkg: PackageJson, path: string, relativePath: string) => string;
80+
pathFilter?: PathFilter;
5181
/** Current root directory. */
5282
rootDir?: string;
5383
};
@@ -79,7 +109,6 @@ const defaultResolver: SyncResolver = (path, options) => {
79109
return pnpResolver(path, options);
80110
}
81111

82-
// @ts-expect-error: TODO remove after merging https://github.com/DefinitelyTyped/DefinitelyTyped/pull/59990
83112
const resolveOptions: UpstreamResolveOptionsWithConditions = {
84113
...options,
85114
isDirectory,
@@ -108,7 +137,7 @@ export default defaultResolver;
108137
* helper functions
109138
*/
110139

111-
function readPackageSync(_: unknown, file: string): PackageJson {
140+
function readPackageSync(_: unknown, file: string): PackageJSON {
112141
return readPackageCached(file);
113142
}
114143

packages/jest-resolve/src/fileWalkers.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import {dirname, resolve} from 'path';
99
import * as fs from 'graceful-fs';
1010
import {tryRealpath} from 'jest-util';
11+
import type {PackageJSON} from './types';
1112

1213
export function clearFsCache(): void {
1314
checkedPaths.clear();
@@ -71,17 +72,15 @@ function realpathCached(path: string): string {
7172
return result;
7273
}
7374

74-
export type PackageJson = Record<string, unknown>;
75-
76-
const packageContents = new Map<string, PackageJson>();
77-
export function readPackageCached(path: string): PackageJson {
75+
const packageContents = new Map<string, PackageJSON>();
76+
export function readPackageCached(path: string): PackageJSON {
7877
let result = packageContents.get(path);
7978

8079
if (result != null) {
8180
return result;
8281
}
8382

84-
result = JSON.parse(fs.readFileSync(path, 'utf8')) as PackageJson;
83+
result = JSON.parse(fs.readFileSync(path, 'utf8')) as PackageJSON;
8584

8685
packageContents.set(path, result);
8786

packages/jest-resolve/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77

88
import Resolver from './resolver';
99

10-
export type {AsyncResolver, SyncResolver} from './defaultResolver';
10+
export type {
11+
AsyncResolver,
12+
SyncResolver,
13+
PackageFilter,
14+
PathFilter,
15+
} from './defaultResolver';
1116
export type {
1217
FindNodeModuleConfig,
1318
ResolveModuleConfig,
1419
ResolverObject as JestResolver,
1520
} from './resolver';
21+
export type {PackageJSON} from './types';
1622
export * from './utils';
1723

1824
export default Resolver;

packages/jest-resolve/src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ type ModuleNameMapperConfig = {
2121
regex: RegExp;
2222
moduleName: string | Array<string>;
2323
};
24+
25+
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
26+
type JSONValue = string | number | boolean | JSONObject | Array<JSONValue>;
27+
interface JSONObject {
28+
[key: string]: JSONValue;
29+
}
30+
31+
export type PackageJSON = JSONObject;

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5373,10 +5373,10 @@ __metadata:
53735373
languageName: node
53745374
linkType: hard
53755375

5376-
"@types/resolve@npm:^1.20.0":
5377-
version: 1.20.1
5378-
resolution: "@types/resolve@npm:1.20.1"
5379-
checksum: d035d5aaadbd455027fa9457a4a263563d49647f088876aebd2b0388d4c35400b85c0382fdc2ec5253c35442abfc2d25431c989345b97fefe26a367811214343
5376+
"@types/resolve@npm:^1.20.2":
5377+
version: 1.20.2
5378+
resolution: "@types/resolve@npm:1.20.2"
5379+
checksum: 61c2cad2499ffc8eab36e3b773945d337d848d3ac6b7b0a87c805ba814bc838ef2f262fc0f109bfd8d2e0898ff8bd80ad1025f9ff64f1f71d3d4294c9f14e5f6
53805380
languageName: node
53815381
linkType: hard
53825382

@@ -13446,7 +13446,7 @@ __metadata:
1344613446
dependencies:
1344713447
"@tsd/typescript": ~4.6.2
1344813448
"@types/graceful-fs": ^4.1.3
13449-
"@types/resolve": ^1.20.0
13449+
"@types/resolve": ^1.20.2
1345013450
chalk: ^4.0.0
1345113451
graceful-fs: ^4.2.9
1345213452
jest-haste-map: ^28.0.0-alpha.11

0 commit comments

Comments
 (0)