Skip to content

Commit 132aa1e

Browse files
committed
Consolidate to just defaultResolver in resolver options
1 parent f7dd81b commit 132aa1e

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
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-6
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,7 +54,7 @@ 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> {
@@ -64,7 +64,7 @@ export function defaultResolverAsync(
6464
// QUESTION: do we need an async version of pnpResolver?
6565
// It seems ugly to require a default sync resolver in the async method,
6666
// just to deal with this.
67-
return Promise.resolve(pnpResolver(path, options));
67+
return Promise.resolve(await pnpResolver(path, options));
6868
}
6969

7070
return new Promise((resolve, reject) => {
@@ -85,7 +85,9 @@ export function defaultResolverAsync(
8585
* getBaseResolveOptions returns resolution options that are shared by both the
8686
* synch and async resolution functions.
8787
*/
88-
function getBaseResolveOptions(options: ResolverOptions) {
88+
function getBaseResolveOptions(
89+
options: ResolverOptions | ResolverOptionsAsync,
90+
) {
8991
return {
9092
basedir: options.basedir,
9193
extensions: options.extensions,
@@ -112,7 +114,7 @@ function getSyncResolveOptions(options: ResolverOptions): SyncOpts {
112114
/**
113115
* getAsyncResolveOptions returns resolution options that are used asynchronously.
114116
*/
115-
function getAsyncResolveOptions(options: ResolverOptions): AsyncOpts {
117+
function getAsyncResolveOptions(options: ResolverOptionsAsync): AsyncOpts {
116118
return {
117119
...getBaseResolveOptions(options),
118120
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)