Skip to content

Commit fe7f28c

Browse files
joshkelcpojer
andauthored
Update resolver docs (#15749)
Co-authored-by: Christoph Nakazawa <[email protected]>
1 parent bbb21a6 commit fe7f28c

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

docs/Configuration.md

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

1525-
By passing a `mainFields` option to `defaultResolver` 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-
mainFields: ['module', 'main'],
1532+
// `unrs-resolver` option: https://github.com/unrs/unrs-resolver#main-field
1533+
mainFields: ['react-native', 'main'],
15331534
});
15341535
};
15351536
```
15361537

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+
15371557
### `restoreMocks` \[boolean]
15381558

15391559
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)