-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom jest resolver to fix export statement syntax errors
- Loading branch information
1 parent
46616f8
commit 5bfd951
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// See: https://github.com/WordPress/gutenberg/blob/61aa916e4e55d9eefa06abff884e3dde9bd1b2eb/test/unit/scripts/resolver.js | ||
module.exports = (path, options) => { | ||
// Call the defaultResolver, so we leverage its cache, error handling, etc. | ||
return options.defaultResolver(path, { | ||
...options, | ||
// Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb) | ||
packageFilter: (pkg) => { | ||
// This is a workaround for https://github.com/uuidjs/uuid/pull/616 | ||
// | ||
// jest-environment-jsdom 28+ tries to use browser exports instead of default exports, | ||
// but uuid v8 only offers an ESM browser export and not a CommonJS one. Jest does not yet | ||
// support ESM modules natively, so this causes a Jest error related to trying to parse | ||
// "export" syntax. | ||
// | ||
// This workaround prevents Jest from considering uuid's module-based exports at all; | ||
// it falls back to uuid's CommonJS+node "main" property. | ||
// | ||
// Once we're able to migrate our Jest config to ESM and a browser crypto | ||
// implementation is available for the browser+ESM version of uuid to use (eg, via | ||
// https://github.com/jsdom/jsdom/pull/3352 or a similar polyfill), this can go away. | ||
if ( | ||
pkg.name === 'uuid' || | ||
pkg.name === 'react-colorful' || | ||
pkg.name === '@eslint/eslintrc' || | ||
pkg.name === 'expect' | ||
) { | ||
delete pkg.exports; | ||
delete pkg.module; | ||
} | ||
return pkg; | ||
}, | ||
}); | ||
}; |