-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
add zero-config support for loading ES modules in Node (round 2) #3703
Conversation
Well, adding zero-config support will be beneficial for the tests, but then we would have to push lots of branches. |
The parsing/loading of modules is a core functionality of Node. I'm unsure wether we should replace this by an external module. I haven't understood the exact difference between using:
Are we considering this PR only to save the typing of Anyway just changing "bin/_mocha" will not do the job. Since we don't spawn a child-process in all cases anymore, "bin/mocha" would need to be adapted as well. |
@juergba good questions, will have a go at answering.
i think it works with .js extension too. Also
So AFAIK we sideload after the core code has run, so while this would work for spec files, it would not work for mochas core code. e.g. (an
It believe it acts like the working example above EXCEPT doesn't apply to the users spec file. So its the best of both. We can use ESM inside core without impacting users code. Added to Let me know your thoughts on above. Its possible im completely out of my depth here lol. |
Where should we allow ESM:
IMO we should enable the first two items. We should go for |
@@ -151,5 +152,5 @@ if (Object.keys(nodeArgs).length) { | |||
proc.kill('SIGTERM'); // if that didn't work, we're probably in an infinite loop, so make it die. | |||
}); | |||
} else { | |||
require('../lib/cli/cli').main(unparse(mochaArgs, {alias: aliases})); | |||
esmRequire('../lib/cli/cli').main(unparse(mochaArgs, {alias: aliases})); |
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.
In my tests this didn't work. Overriding require
with require = require('esm')(module);
on L13 does work though.
Maybe there are more solutions.
Just loading the module esm
as we do it in lib/cli/run-helpers.js is not working, additionally the require
function has to be overridden, somewhere ...
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.
In my tests this didn't work.
How were you testing? Seemed ok for me (for core code), been testing w/o any node flags tho.
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.
I just tested this test file:
import {ok} from 'assert';
describe('fixture', function() {
it('should be true', function() {
ok(true);
});
});
It does not run with the changes of this PR. It does run with --require esm
.
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 I think esmRequire
only gets applied to core code, so thats sounds right.
Keeping up-to-date with language standards w/o a compilation-step feels a big benefit. Even once native ESM lands will be some time before we can drop support for un-supporting envs. But its disappointing we have had so many issues (and concerns) with it. |
We are mainly still vegetating in ES5 caves. I feel rather uncomfortable about this issue. It's not enough to take a few minutes time to say "it" runs and then merge this PR. #3006 is a good beginning to understand what our users' expectations are. |
closed in lieu of #4038 |
This PR adds zero-config support for loading ES modules in Node. This PR just replaces the entry
require
withesmRequire
. Now--require
modules, configs, and others files can have ESM syntax. The caveat is that.mjs
files are not supported since under the hood mocha is still usingrequire()
to load files. See the followingesm
doc note:@boneskull Feel free to push tests to this PR.