-
Notifications
You must be signed in to change notification settings - Fork 338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Babel transform only *.mjs
files for Jest
#3851
Conversation
dc5f62c
to
545532c
Compare
I like that, that sounds a very “working with how Node works” way to sort ourselves out 🙌🏻 Two little questions:
|
Thanks @romaricpascal Using ESLint
Yeah we could add ESLint rules but you can't identify Puppeteer tests by filename Puppeteer tests with ReferenceError: _interopRequireWildcard is not defined
pptr:evaluate;Object.%3Canonymous%3E%20(%2FUsers%2Fcolin%2FSites%2FGDS%2Fgovuk-frontend%2Fpackages%2Fgovuk-frontend%2Fsrc%2Fgovuk%2Fcomponents%2Fglobals.test.mjs%3A13%3A29):1:61 Other tests with Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax. ESM-only packages
Babel doesn't transpile code in
Yeah they use
Simplest way to opt in to Babel transpiling would be to use the |
There are also Jest environments that avoid Node.js VM contexts like: |
545532c
to
cb2624b
Compare
cb2624b
to
11ec0f1
Compare
*.mjs
files for Jest*.mjs
files for Jest
11ec0f1
to
bfafd2f
Compare
0ab4cf3
to
bfafd2f
Compare
bfafd2f
to
62c1f76
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! 🙌🏻 There's a reference to globals.test.mjs
in testing-and-linting.md
which could use updating as part of 84f3263. And I think it'd be worth highlighting the reason for picking a .js
file over a .mjs
(.mjs
by default, unless you need to do a dynamic import()
in the file, is that it?). Would you mind adding that, please?
@@ -60,6 +60,7 @@ const config = { | |||
*/ | |||
export default { | |||
collectCoverageFrom: ['./packages/govuk-frontend/src/**/*.{js,mjs}'], | |||
coverageProvider: 'v8', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question Does that affect the coverage of the tests that do run through Babel? I think what I'm worried about is the Babel helpers being considered for the coverage if it is collected via v8
, but excluded with default setting. I wouldn't block the PR as we're not looking much at coverage at the moment, but it'll be something to dig into if/when we get to #3101 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah a little bit
Coverage from coverageProvider: 'v8'
is generated from transpiled code but it's mapped back to the original source using source maps and will exclude sneaky helpers. Read about when it launched in Jest v25
I'm sure we'll see minor differences in coverage percentages. Turning off Babel transforms for *.js
stops babel-plugin-istanbul
from instrumenting the code so it's better than losing it entirely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The situation doesn't sound too bad if the v8
thing takes sourcemaps into account. Like I said, not a blocker, just wanted to understand a bit and flag for #3101 😊
This reverts commit e5399f9.
This reverts commit c558637.
Previously we excluded Babel `transform-dynamic-import` but we realised that some tests DO actually need to transform `import()` to `require()` or they hit Jest limitations: >You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules By switching to CommonJS we can continue to pass `import('govuk-frontend')` into Puppeteer but avoid Babel transforms that run on ES modules
We can collect coverage using the Node.js V8 engine to avoid running every file through Babel
We don’t need to transform `*.js` files as they’re typically supported by Jest already, but we’ll add another commit to handle some exceptions
We need to include ESM-only modules and in future any files with dynamic `import()` so Jest can run them as compatible CommonJS
62c1f76
to
c525c29
Compare
Thanks @romaricpascal
✅ Done
✅ Done I've gone with the text below as it's only dynamic
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat! Thanks for updating the docs 🥳 ⛵
Quick spike to solve to blockers in #3812
import()
to load in a browserimport()
withrequire()
for virtual machine contextsBy running Babel only on
*.mjs
ES modules we can use dynamicimport()
in*.js
CommonJSUpdate: Gave it a couple of runs on Windows to confirm it works: