Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1522,18 +1522,38 @@ const config: Config = {
export default config;
```

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"`:
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:

```js
module.exports = (path, options) => {
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, {
...options,
mainFields: ['module', 'main'],
// `unrs-resolver` option: https://github.com/unrs/unrs-resolver#main-field
mainFields: ['react-native', 'main'],
});
};
```

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.

```js
module.exports = (path, options) => {
// Dynamic imports within our codebase that reference .js need to reference
// .ts during tests.
if (
!options.basedir.includes('node_modules') &&
path.endsWith('.js') &&
(path.startsWith('../') || path.startsWith('./'))
) {
path = path.replace(/\.js$/, '.ts');
}

// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, options);
};
```

### `restoreMocks` \[boolean]

Default: `false`
Expand Down
21 changes: 20 additions & 1 deletion website/versioned_docs/version-30.0/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1536,12 +1536,31 @@ module.exports = (path, options) => {
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, {
...options,
// See this `unrs-resolver` option: from https://github.com/unrs/unrs-resolver?tab=readme-ov-file#main-field
// `unrs-resolver` option: https://github.com/unrs/unrs-resolver#main-field
mainFields: ['react-native', 'main'],
});
};
```

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.

```js
module.exports = (path, options) => {
// Dynamic imports within our codebase that reference .js need to reference
// .ts during tests.
if (
!options.basedir.includes('node_modules') &&
path.endsWith('.js') &&
(path.startsWith('../') || path.startsWith('./'))
) {
path = path.replace(/\.js$/, '.ts');
}

// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, options);
};
```

### `restoreMocks` \[boolean]

Default: `false`
Expand Down
Loading