Skip to content

Commit 179224d

Browse files
committed
Consolidate to just defaultResolver in resolver options
1 parent f7dd81b commit 179224d

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

docs/Configuration.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,7 @@ The options object provided to resolvers has the shape:
793793
```json
794794
{
795795
"basedir": string,
796-
"defaultResolver": "function(request, options) -> string",
797-
"defaultResolverAsync": "function(request, options) -> Promise<string>",
796+
"defaultResolver": "function(request, options) -> string | Promise<string>",
798797
"extensions": [string],
799798
"moduleDirectory": [string],
800799
"paths": [string],
@@ -803,7 +802,7 @@ The options object provided to resolvers has the shape:
803802
}
804803
```
805804

806-
Note: the defaultResolver passed as an option is the Jest default resolver which might be useful when you write your custom one. It takes the same arguments as your custom one, e.g. `(request, options)`.
805+
Note: the defaultResolver passed as an option is the Jest default resolver which might be useful when you write your custom one. It takes the same arguments as your custom one, e.g. `(request, options)` and returns a string for sync resolvers and a promise for async resolvers.
807806

808807
For example, if you want to respect Browserify's [`"browser"` field](https://github.com/browserify/browserify-handbook/blob/master/readme.markdown#browser-field), you can use the following configuration:
809808

packages/jest-resolve/src/defaultResolver.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export type ResolverOptions = {
2424
packageFilter?: (pkg: any, pkgfile: string) => any;
2525
};
2626

27-
type ResolverOptionsAsync = ResolverOptions & {
28-
defaultResolverAsync: typeof defaultResolverAsync;
27+
type ResolverOptionsAsync = Omit<ResolverOptions, 'defaultResolver'> & {
28+
defaultResolver: typeof defaultResolverAsync;
2929
};
3030

3131
// https://github.com/facebook/jest/pull/10617
@@ -54,17 +54,14 @@ export function defaultResolver(
5454
return realpathSync(result);
5555
}
5656

57-
export function defaultResolverAsync(
57+
export async function defaultResolverAsync(
5858
path: Config.Path,
5959
options: ResolverOptionsAsync,
6060
): Promise<Config.Path> {
6161
// Yarn 2 adds support to `resolve` automatically so the pnpResolver is only
6262
// needed for Yarn 1 which implements version 1 of the pnp spec
6363
if (process.versions.pnp === '1') {
64-
// QUESTION: do we need an async version of pnpResolver?
65-
// It seems ugly to require a default sync resolver in the async method,
66-
// just to deal with this.
67-
return Promise.resolve(pnpResolver(path, options));
64+
return Promise.resolve(await pnpResolver(path, options));
6865
}
6966

7067
return new Promise((resolve, reject) => {
@@ -85,7 +82,9 @@ export function defaultResolverAsync(
8582
* getBaseResolveOptions returns resolution options that are shared by both the
8683
* synch and async resolution functions.
8784
*/
88-
function getBaseResolveOptions(options: ResolverOptions) {
85+
function getBaseResolveOptions(
86+
options: ResolverOptions | ResolverOptionsAsync,
87+
) {
8988
return {
9089
basedir: options.basedir,
9190
extensions: options.extensions,
@@ -112,7 +111,7 @@ function getSyncResolveOptions(options: ResolverOptions): SyncOpts {
112111
/**
113112
* getAsyncResolveOptions returns resolution options that are used asynchronously.
114113
*/
115-
function getAsyncResolveOptions(options: ResolverOptions): AsyncOpts {
114+
function getAsyncResolveOptions(options: ResolverOptionsAsync): AsyncOpts {
116115
return {
117116
...getBaseResolveOptions(options),
118117
isDirectory: isDirectoryAsync,

packages/jest-resolve/src/resolver.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ export default class Resolver {
163163
const result = await resolver(path, {
164164
basedir: options.basedir,
165165
browser: options.browser,
166-
defaultResolver,
167-
defaultResolverAsync,
166+
defaultResolver: defaultResolverAsync,
168167
extensions: options.extensions,
169168
moduleDirectory: options.moduleDirectory,
170169
paths: paths ? (nodePaths || []).concat(paths) : nodePaths,

0 commit comments

Comments
 (0)