You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Configuration.md
+22-2Lines changed: 22 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1522,18 +1522,38 @@ const config: Config = {
1522
1522
exportdefaultconfig;
1523
1523
```
1524
1524
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:
1526
1526
1527
1527
```js
1528
1528
module.exports= (path, options) => {
1529
1529
// Call the defaultResolver, so we leverage its cache, error handling, etc.
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.
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.
0 commit comments