-
-
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
Support ES6 style tests without transpiler usage #3006
Comments
Initial thoughts off the top of my head, before doing any further research:
|
From my (little) research it seems that at least for now it is allowed to use
Yes, it was run with
The issue seems to be, and I might be missing something here, that mocha uses
I was afraid that this would be the answer and I understand that this is not a top priority. If my assumption of the cause of the error above is correct, soemthing like #956 could help as the entry point of the test could be a |
To clarify: given that in "older" (in some cases current non-experimental) environments
Thanks, that eliminates one possible angle!
Making Mocha not create global variables is unfortunately not possible without extensive rewriting of some of the more arcane internals (I tried and couldn't figure it out myself 😿 ); however, using your own JS entry point is possible now through the "programmatic" API (which might not be documented outside of an old wiki page and the JSDoc comments in the source files, but is officially supported): // test.mjs
var Mocha = require('mocha'),
var mocha = new Mocha();
// your mission: create a file `example.mjs`
// in the `test` folder and have it `import` stuff
// as well as using `describe` and `it` to make tests
mocha.addFile("./test/example.mjs");
mocha.run(function(failures){
process.on('exit', function () {
process.exit(failures ? 1 : 0);
});
}); node --experimental-modules test.mjs I haven't actually tried that to see if it makes a difference (need to grab the latest version of Node first), but let me know if it works for you... |
First of all thank you for your support on this! I tried this
So basically made the node entry point a ECMAScript module. I run it via I get
That I'm afraid is currently not possible in node, you can only use
? |
There's no function like that in Mocha at the moment, but you can do something along those lines. Lines 220 to 235 in 1cc0fc0
What you'd want to do is for any files that need to be modules.forEach(function (file) {
file = path.resolve(file);
mocha.suite.emit('pre-require', global, file, mocha);
import fileExport from file; // fileExport is used by the exports interface, not sure if anything else; most interfaces act as a side effect of running the file
mocha.suite.emit('require', fileExport, file, mocha);
mocha.suite.emit('post-require', global, file, mocha);
}); You can also look at how https://github.com/mochajs/mocha/blob/master/bin/_mocha uses Mocha's programmatic API to get a sense of how to supply other options and how to use things like Mocha's file lookup functionality. It's not very well organized but everything the commandline interface does is in there (either directly or because in there is a call to the functions in Mocha's programmatic API). |
I can come one step further but the imported test now complains it does not know about describe (
it complains |
So, my distro finally got NodeJS 8.5, and I've had a chance to play with this and confirm a couple of hunches I had but didn't want to state till I'd been able to check:
However, I did discover I can make it work by exploiting the fact that https://gist.github.com/anonymous/caba0883254a2349c5615df8e9286665 node --experimental-modules ./run.mjs Unfortunately, I'm fairly sure that's the best we can do given the way ES modules work and what Node allows at the present time. |
Think of it another way:
You cannot dynamically There is this stage 3 proposal which would allow this behavior, but I'm not sure if any runtimes are shipping it yet. As such, there's no way for Mocha to import an
|
Actually, it occurs to me that we could load the tests and leverage |
(then again, this may be exactly what |
I've got mocha tests working without transpiler in a browser. Maybe it helps for this issue. |
that’s unrelated as you’re not pulling mocha in as a module, but rather a script... |
sorry confused myself. it’s different in a browser. |
I want to weigh in with a vote of support for doing something to allow Mocha to run tests located in an ES Module. I landed here after trying to write such a test and getting a weirdo error from the Node.js module loader. I'm using Node.js 9.5, which natively supports ES6 modules. As it currently stands, Node.js 9.5 does not allow a CommonJS module to require() an ES6 module. Maybe they're working in the direction of allowing that, I don't know. I wrote the test as a ES6 module with the Redid the test with the I'm of the opinion that the Node.js world needs to consider how they'll move to and support ES6 modules. Since Mocha is a very popular tool in this world, it would be best for the Mocha team to consider how to support ES6 modules. |
To follow up ... After some pondering and searching I was able to get this sequence to work as a workaround. Name the test script with Then add this in the test script:
Then I can
|
@robogeek Or it might be even better to use the
|
Dynamic import ships with node v9.6 behind the |
@harrysarson It is not going to work out of the box. Mocha uses cjs modules and |
I am a bot that watches issues for inactivity. |
The issue is still relevant but relies on native support for ESM. Browsers have it, Node not yet. |
I was just playing around, getting familiar with ESM/.mjs and decided I needed tests for my toy. Realizing mocha is not yet officially supporting https://www.npmjs.com/package/mocha-esm There might be something better out there, but it was fun to through together. so \o/ |
I think this is still relevant. |
Was anybody able to run Mocha with ES6 Modules on (edit: (async () => {
await import("./tests.js");
run();
})(); Then But for some unknown reason, on 12.11.0, it behaves as if there would be no
|
@tomalec I am running mocha with ES modules on node 12.11.1: mocha-run.js (async () => {
await import("./tests.mjs");
run();
})(); However, watch mode is not working. mocha waits for file changes, but doesn't run another test run after a file has been changed. |
@vanslly Lucky you ;)
|
That this is still an issue is crazy! ESM is no longer hidden away behind --experimental-modules. This is the future. |
err, actually it was just announced a couple days ago... |
This comment has been minimized.
This comment has been minimized.
Hey guys, just want to make sure this is kept alive. Please make this a top priority and thanks for all the great work! |
We published yesterday an experimental release |
This is great to see! Can I ask though--does the lack of reference to the browser mean that ESM usage is not available on the browser or merely that you haven't needed to specify browser versions as with Node. I think it might help to mention in the release notes the status for browsers (and if not supported, what the plans might be for its support). |
@brettz9 If I remember well, you have to set the |
@juergba : yes, sure, but one needs an ESM export distribution file so one can use such as |
mocha is written in commonjs; we cannot put a “module” field in package.json. Mocha will support running tests in node written in ESM. |
If you didn't want to refactor to use ESM internally, you should still be able to use Rollup with its CommonJS plugin and indicate the ESM target file in order to support |
I’ve created a sample project to test mocha with ESM. I can successfully run the tests, but wasn’t (yet) able to run coverage with nyc/istanbul. Your help will be welcome. |
@concatime Until
|
@cedx I’ve updated my template repo., and it works. Neat! |
We implemented Node's native ESM support in Mocha v7.1.0. |
Prerequisites
common mistake
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend avoiding the use of globally installed Mocha.Description
Before I start, there are already some closed issues regarding this topic but as the prerequisites have changed I would like to start a new attempt.
Now that node supports running EMCAScript modules (yes, I know it is experimental) it would be great to see mocha to work in conjunction with
mjs
test definitions.Steps to Reproduce
I have a very simple test
Which i have saved as
test.js
andtest.mjs
Expected behavior: I would like both tests to show
Actual behavior: While the
js
test works, themjs
test gives meReproduces how often: 100%
Versions
node --version
- v8.5.0mocha --version
- 3.5.3Additional Information
I think that this might be that mocha's runner is using commonjs and nodejs' current implementation disallows to use ECMAScript modules from a commonjs context.
Please don't reply with "use a transpiler", I want to explicitly not use one.
Edit: in an earlier version I accidentally used jsx instead of mjs.
The text was updated successfully, but these errors were encountered: