-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Handle ERR_REQUIRE_ESM when reading configuration #9573
Conversation
Codecov Report
@@ Coverage Diff @@
## master #9573 +/- ##
==========================================
- Coverage 65.04% 65.03% -0.02%
==========================================
Files 286 286
Lines 12140 12139 -1
Branches 3008 3008
==========================================
- Hits 7897 7895 -2
Misses 3605 3605
- Partials 638 639 +1
Continue to review full report at Codecov.
|
I think I read somewhere that this will print a warning to the console. Does it? If not this looks ideal 🙂 |
In my test script on Node 13.8 I get the warning:
But doesn't that happen with the code on |
test.cjs: async function test() {
let mod;
try {
mod = require('./config.js');
console.log('require worked');
} catch (e) {
if (e.code === 'ERR_REQUIRE_ESM') {
console.log('falling back to import()');
mod = (await import('./config.js')).default;
}
}
console.log(mod);
}
test(); config.js: export default { foo: 'bar' }; package.json: {"type":"module"} $ docker run --rm -it -v "$(pwd):/tmp" -w /tmp node:13 node test
falling back to import()
(node:1) ExperimentalWarning: The ESM module loader is experimental.
{ foo: 'bar' } |
Perfect, thanks for checking! 🙂 Just a test and we can merge this then 👍 |
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.
thank you!
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
#9431 added support for
jest.config.mjs
files.This PR attempts to support
jest.config.js
files that are ES modules, when a project'spackage.json
has"type": "module"
.Current behaviour (Jest 25.1.0):
.mjs
, doawait import(configPath)
.require(configPath)
.Proposed behaviour:
require(configPath)
ERR_REQUIRE_ESM
is thrown, doawait import(configPath)
.This will leave the module detection logic to Node.js, in lieu of an API to query it (nodejs/node#49446).
It will be compatible with older versions of Node (8), and should be somewhat resilient to changes in the modules spec.
Test plan
I'm opening this PR as a discussion, so I haven't added any tests just yet.
There's already some discussion here.