jest.resolver.cjs 1.4 KB

123456789101112131415161718192021222324252627
  1. module.exports = (path, options) => {
  2. // Call the defaultResolver, so we leverage its cache, error handling, etc.
  3. return options.defaultResolver(path, {
  4. ...options,
  5. // Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
  6. packageFilter: (pkg) => {
  7. // This is a workaround for https://github.com/uuidjs/uuid/pull/616
  8. //
  9. // jest-environment-jsdom 28+ tries to use browser exports instead of default exports,
  10. // but uuid only offers an ESM browser export and not a CommonJS one. Jest does not yet
  11. // support ESM modules natively, so this causes a Jest error related to trying to parse
  12. // "export" syntax.
  13. //
  14. // This workaround prevents Jest from considering uuid's module-based exports at all;
  15. // it falls back to uuid's CommonJS+node "main" property.
  16. //
  17. // Once we're able to migrate our Jest config to ESM and a browser crypto
  18. // implementation is available for the browser+ESM version of uuid to use (eg, via
  19. // https://github.com/jsdom/jsdom/pull/3352 or a similar polyfill), this can go away.
  20. if (pkg.name === 'uuid') {
  21. delete pkg['exports'];
  22. delete pkg['module'];
  23. }
  24. return pkg;
  25. },
  26. });
  27. };