diff --git a/jest.config.mjs b/jest.config.mjs index fd16677d0c..e518ec8740 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -1,3 +1,6 @@ +import { packageResolveToPath } from 'govuk-frontend-lib/names' +import { replacePathSepForRegex } from 'jest-regex-util' + import jestPuppeteerConfig from './jest-puppeteer.config.js' // Detect when browser has been launched headless @@ -35,7 +38,20 @@ const config = { // Enable Babel transforms until Jest supports ESM and `import()` // See: https://jestjs.io/docs/ecmascript-modules transform: { - '^.+\\.mjs$': ['babel-jest', { rootMode: 'upward' }] + // Transform all `*.mjs` to compatible CommonJS + '^.+\\.mjs$': ['babel-jest', { + rootMode: 'upward' + }], + + // Transform some `*.js` to compatible CommonJS + ...Object.fromEntries([ + 'del', + 'slash' + ].map((packagePath) => [ + replacePathSepForRegex(`${packageResolveToPath(packagePath)}$`), ['babel-jest', { + rootMode: 'upward' + }] + ])) }, // Enable Babel transforms for ESM-only node_modules diff --git a/package-lock.json b/package-lock.json index d152bc3f86..5320fee5af 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,6 +39,7 @@ "jest": "^29.5.0", "jest-environment-node-single-context": "^29.1.0", "jest-puppeteer": "^9.0.0", + "jest-regex-util": "^29.4.3", "jest-serializer-html": "^7.1.0", "lint-staged": "^13.2.2", "postcss-markdown": "^1.2.0", diff --git a/package.json b/package.json index 541239023b..4d59f25443 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "jest": "^29.5.0", "jest-environment-node-single-context": "^29.1.0", "jest-puppeteer": "^9.0.0", + "jest-regex-util": "^29.4.3", "jest-serializer-html": "^7.1.0", "lint-staged": "^13.2.2", "postcss-markdown": "^1.2.0",