Skip to content

Commit d880717

Browse files
committed
Update resolver docs
The `packageFilter` option to `defaultResolver` appears to have gone away as part of #15619. The versioned 30.0 docs were correctly updated, but the "Next" docs were not. Shorten the URL comment to make it less likely to induce scrolling. I also added another example of a resolver that I've found useful in my own project. Feedback/corrections welcome.
1 parent 22236cf commit d880717

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

docs/Configuration.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,25 +1522,38 @@ const config: Config = {
15221522
export default config;
15231523
```
15241524

1525-
By combining `defaultResolver` and `packageFilter` we can implement a `package.json` "pre-processor" that allows us to change how the default resolver will resolve modules. For example, imagine we want to use the field `"module"` if it is present, otherwise fallback to `"main"`:
1525+
Jest's `jest-resolve` relies on `unrs-resolver`. We can pass additional options, for example modifying `mainFields` for resolution. For example, for React Native projects, you might want to use this config:
15261526

15271527
```js
15281528
module.exports = (path, options) => {
15291529
// Call the defaultResolver, so we leverage its cache, error handling, etc.
15301530
return options.defaultResolver(path, {
15311531
...options,
1532-
// Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
1533-
packageFilter: pkg => {
1534-
return {
1535-
...pkg,
1536-
// Alter the value of `main` before resolving the package
1537-
main: pkg.module || pkg.main,
1538-
};
1539-
},
1532+
// `unrs-resolver` option: https://github.com/unrs/unrs-resolver#main-field
1533+
mainFields: ['react-native', 'main'],
15401534
});
15411535
};
15421536
```
15431537

1538+
You can also use `defaultResolver` to implement a "pre-processor" that allows us to change how the default resolver will resolve modules. For example, suppose a TypeScript project needs to reference `.js` files at runtime but runs Jest on the `.ts` files.
1539+
1540+
```js
1541+
module.exports = (path, options) => {
1542+
// Dynamic imports within our codebase that reference .js need to reference
1543+
// .ts during tests.
1544+
if (
1545+
!options.basedir.includes('node_modules') &&
1546+
path.endsWith('.js') &&
1547+
(path.startsWith('../') || path.startsWith('./'))
1548+
) {
1549+
path = path.replace(/\.js$/, '.ts');
1550+
}
1551+
1552+
// Call the defaultResolver, so we leverage its cache, error handling, etc.
1553+
return options.defaultResolver(path, options);
1554+
};
1555+
```
1556+
15441557
### `restoreMocks` \[boolean]
15451558

15461559
Default: `false`

website/versioned_docs/version-30.0/Configuration.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,12 +1536,31 @@ module.exports = (path, options) => {
15361536
// Call the defaultResolver, so we leverage its cache, error handling, etc.
15371537
return options.defaultResolver(path, {
15381538
...options,
1539-
// See this `unrs-resolver` option: from https://github.com/unrs/unrs-resolver?tab=readme-ov-file#main-field
1539+
// `unrs-resolver` option: https://github.com/unrs/unrs-resolver#main-field
15401540
mainFields: ['react-native', 'main'],
15411541
});
15421542
};
15431543
```
15441544

1545+
You can also use `defaultResolver` to implement a "pre-processor" that allows us to change how the default resolver will resolve modules. For example, suppose a TypeScript project needs to reference `.js` files at runtime but runs Jest on the `.ts` files.
1546+
1547+
```js
1548+
module.exports = (path, options) => {
1549+
// Dynamic imports within our codebase that reference .js need to reference
1550+
// .ts during tests.
1551+
if (
1552+
!options.basedir.includes('node_modules') &&
1553+
path.endsWith('.js') &&
1554+
(path.startsWith('../') || path.startsWith('./'))
1555+
) {
1556+
path = path.replace(/\.js$/, '.ts');
1557+
}
1558+
1559+
// Call the defaultResolver, so we leverage its cache, error handling, etc.
1560+
return options.defaultResolver(path, options);
1561+
};
1562+
```
1563+
15451564
### `restoreMocks` \[boolean]
15461565

15471566
Default: `false`

0 commit comments

Comments
 (0)