diff --git a/docs/Configuration.md b/docs/Configuration.md index 3368054a97ae..967183a0ae8f 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -793,8 +793,7 @@ The options object provided to resolvers has the shape: ```json { "basedir": string, - "defaultResolver": "function(request, options) -> string", - "defaultResolverAsync": "function(request, options) -> Promise", + "defaultResolver": "function(request, options) -> string | Promise", "extensions": [string], "moduleDirectory": [string], "paths": [string], @@ -803,7 +802,7 @@ The options object provided to resolvers has the shape: } ``` -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)`. +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. 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: diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 63c8f56aa69a..d16dc4547555 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -24,8 +24,8 @@ export type ResolverOptions = { packageFilter?: (pkg: any, pkgfile: string) => any; }; -type ResolverOptionsAsync = ResolverOptions & { - defaultResolverAsync: typeof defaultResolverAsync; +type ResolverOptionsAsync = Omit & { + defaultResolver: typeof defaultResolverAsync; }; // https://github.com/facebook/jest/pull/10617 @@ -54,7 +54,7 @@ export function defaultResolver( return realpathSync(result); } -export function defaultResolverAsync( +export async function defaultResolverAsync( path: Config.Path, options: ResolverOptionsAsync, ): Promise { @@ -64,7 +64,7 @@ export function defaultResolverAsync( // QUESTION: do we need an async version of pnpResolver? // It seems ugly to require a default sync resolver in the async method, // just to deal with this. - return Promise.resolve(pnpResolver(path, options)); + return Promise.resolve(await pnpResolver(path, options)); } return new Promise((resolve, reject) => { @@ -85,7 +85,9 @@ export function defaultResolverAsync( * getBaseResolveOptions returns resolution options that are shared by both the * synch and async resolution functions. */ -function getBaseResolveOptions(options: ResolverOptions) { +function getBaseResolveOptions( + options: ResolverOptions | ResolverOptionsAsync, +) { return { basedir: options.basedir, extensions: options.extensions, @@ -112,7 +114,7 @@ function getSyncResolveOptions(options: ResolverOptions): SyncOpts { /** * getAsyncResolveOptions returns resolution options that are used asynchronously. */ -function getAsyncResolveOptions(options: ResolverOptions): AsyncOpts { +function getAsyncResolveOptions(options: ResolverOptionsAsync): AsyncOpts { return { ...getBaseResolveOptions(options), isDirectory: isDirectoryAsync, diff --git a/packages/jest-resolve/src/resolver.ts b/packages/jest-resolve/src/resolver.ts index 2ec7a67d3f1f..f1cefa6cf087 100644 --- a/packages/jest-resolve/src/resolver.ts +++ b/packages/jest-resolve/src/resolver.ts @@ -163,8 +163,7 @@ export default class Resolver { const result = await resolver(path, { basedir: options.basedir, browser: options.browser, - defaultResolver, - defaultResolverAsync, + defaultResolver: defaultResolverAsync, extensions: options.extensions, moduleDirectory: options.moduleDirectory, paths: paths ? (nodePaths || []).concat(paths) : nodePaths,