From db4f62187c64a73307bee0e8451c285cf5abdecf Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Sat, 6 May 2023 18:46:39 +0530 Subject: [PATCH] Add custom jest resolver to fix export statement syntax errors See: https://github.com/WordPress/gutenberg/pull/47388#issuecomment-1534502094 --- tests/js/jest.config.js | 1 + tests/js/resolver.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/js/resolver.js diff --git a/tests/js/jest.config.js b/tests/js/jest.config.js index efebe06fb39..b2b2cc052d8 100644 --- a/tests/js/jest.config.js +++ b/tests/js/jest.config.js @@ -1,6 +1,7 @@ module.exports = { rootDir: '../../', ...require('@wordpress/scripts/config/jest-unit.config'), + resolver: '/tests/js/resolver.js', transform: { '^.+\\.[jt]sx?$': '/node_modules/@wordpress/scripts/config/babel-transform', diff --git a/tests/js/resolver.js b/tests/js/resolver.js new file mode 100644 index 00000000000..a1f00cc5905 --- /dev/null +++ b/tests/js/resolver.js @@ -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; + }, + }); +};