Skip to content

Commit

Permalink
Babel transform some *.js files for Jest
Browse files Browse the repository at this point in the history
We need to include ESM-only modules and in future any files with dynamic `import()` so Jest can run them as compatible CommonJS
  • Loading branch information
colinrotherham committed Jun 26, 2023
1 parent 6a75b6c commit 11ec0f1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
18 changes: 17 additions & 1 deletion jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 11ec0f1

Please sign in to comment.