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
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.
Copy file name to clipboardExpand all lines: docs/Configuration.md
+22-9Lines changed: 22 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1522,25 +1522,38 @@ const config: Config = {
1522
1522
exportdefaultconfig;
1523
1523
```
1524
1524
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:
1526
1526
1527
1527
```js
1528
1528
module.exports= (path, options) => {
1529
1529
// Call the defaultResolver, so we leverage its cache, error handling, etc.
1530
1530
returnoptions.defaultResolver(path, {
1531
1531
...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
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