Skip to content

Commit

Permalink
Consolidate to just defaultResolver in resolver options
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Aug 31, 2021
1 parent f7dd81b commit 132aa1e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
5 changes: 2 additions & 3 deletions docs/Configuration.md
Expand Up @@ -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<string>",
"defaultResolver": "function(request, options) -> string | Promise<string>",
"extensions": [string],
"moduleDirectory": [string],
"paths": [string],
Expand All @@ -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:

Expand Down
14 changes: 8 additions & 6 deletions packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -24,8 +24,8 @@ export type ResolverOptions = {
packageFilter?: (pkg: any, pkgfile: string) => any;
};

type ResolverOptionsAsync = ResolverOptions & {
defaultResolverAsync: typeof defaultResolverAsync;
type ResolverOptionsAsync = Omit<ResolverOptions, 'defaultResolver'> & {
defaultResolver: typeof defaultResolverAsync;
};

// https://github.com/facebook/jest/pull/10617
Expand Down Expand Up @@ -54,7 +54,7 @@ export function defaultResolver(
return realpathSync(result);
}

export function defaultResolverAsync(
export async function defaultResolverAsync(
path: Config.Path,
options: ResolverOptionsAsync,
): Promise<Config.Path> {
Expand All @@ -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) => {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-resolve/src/resolver.ts
Expand Up @@ -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,
Expand Down

0 comments on commit 132aa1e

Please sign in to comment.