-
-
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
🚀 Feature: Support ESM (ECMAScript Modules) in watch mode #4374
Comments
"type": "module"
in Project's package.json
--watch
insists on CJS, Ignores "type": "module"
in project's package.json
Please see our docs: current limitations. We do not (yet?) support ESModules in |
--watch
insists on CJS, Ignores "type": "module"
in project's package.json
@Swivelgames Have you tried |
@boneskull I'm not @Swivelgames but your suggestion of adding --parallel works for me. Thanks! |
To support ESM in watch mode, I think you will need to implement your own ESM loader hook. It can append query params to each imported module, effectively making them unique. By appending a different query param the second time, you can ensure a fresh copy of the module is imported. This may, however, be a memory leak, if node has no way to clear the previously-imported copies of those modules. It is probably worth sharing this use-case with the node maintainers so they can add the necessary API surface to node. https://github.com/nodejs/modules/issues EDIT: I see this was already raised with the node team: nodejs/node#49442 |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Based on the comment of @cspotcode and the experiment by @vsnthdev in nodejs/node#49442, I've gone ahead and implemented this as a proof of concept for To run mocha in this mode, you simply pass the This may not be ready for primetime yet, but it works well for my use-case and it shows a viable path forward for ESM. Perhaps it can be wrapped up for release? |
I've just made my branch work for NPM installs, so if you want to test the esm watch mode in your project, you can replace the mocha dependency: yarn add -D mocha@https://github.com/lehni/mocha#99d5a14d3a1b11f81dbb08e1895d43061e7b0541
# Or:
npm install -D https://github.com/lehni/mocha#99d5a14d3a1b11f81dbb08e1895d43061e7b0541 And after that, simply run your test command with yarn test --watch --esm Please let me know how this is working for you! |
I've added auto-detection of esm watch mode now, so the |
Just fyi as of May 2022 this is still an open issue and quite a significant impediment. As an interim workaround I ended up with nodemon in front of Mocha, which works flawlessly and doesn't have a memleak issue mentioned in the PR 👆. As a reference, here's the {
"exec": "npx mocha --node-option=experimental-network-imports",
"watch": [
"./out/**/*"
],
"delay": 100,
"env": {
"NODE_ENV": "test"
}
} |
Possible workaround, using fswatch: fswatch lib/ test/ --event Updated | xargs -I _ mocha test/file.js -b |
Having to use
in package.json |
Another simple workaround is to use entr to watch your source files: {
"scripts": {
"test:watch": "ls lib/*.js | entr -r mocha"
}
} |
mocha + sinon + chai. ESM + typescript is hard :-/ Editor TS, build TS and spec TS don't want to agree on module imports. On top of that — Jakefile can not be ESM yet. So src/ and /spec are modules, and project is not. --watch won't work unless ran with --parallel mochajs/mocha#4374 mochajs/mocha-examples#47 https://gist.github.com/jordansexton/2a0c3c360aa700cc9528e89620e82c3d
mocha + sinon + chai. ESM + typescript is hard :-/ Editor TS, build TS and spec TS don't want to agree on module imports. On top of that — Jakefile can not be ESM yet. So src/ and /spec are modules, and project is not. --watch won't work unless ran with --parallel mochajs/mocha#4374 mochajs/mocha-examples#47 https://gist.github.com/jordansexton/2a0c3c360aa700cc9528e89620e82c3d
I've tried @boneskull solution and combined the |
I found one pretty simple solution for typescript and esm. I use |
This is now possible using a new flag from node 22. This should work: mocha --watch --experimental-require-module Here is the person who found the "fix" The docs Here is where I first saw the merge |
- We can't use `mocha --watch` with ESM modules. However, it seems possible with --parallel, see this comment: mochajs/mocha#4374 (comment)
Not sure if this has been mentioned before, but |
Prerequisites
faq
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
When running
--watch
within a package that has"type": "module"
set in itspackage.json
, Mocha fails to properlyimport
, instead attempting torequire()
the file.Steps to Reproduce
For convenience, the following respository was created that adequately reproduces the issue:
npm i
npm run test-mocha
npm run test-mocha:watch
Expected Behavior:
Actual Behavior:
Versions
Package Versions
Environment
Additional Information
It is my understanding that the following scenarios are applicable:
import
should be used under the following conditions:package.json
file contains"type": "module"
and its file extension is.js
.mjs
, regardless of what is inside its associatedpackage.json
filerequire()
should be used under the following conditions:package.json
file does NOT contain"type": "module"
and its file extension is.js
.cjs
, regardless of what is inside its associatedpackage.json
fileConclusion
Presently, it appears as though
--watch
is assuming the target file is a CommonJS module when its extension is.js
, and failing to yield to itspackage.json
file's"type": "module"
.Considerations
It's worth considering the fact that the
package.json
file associated with the file being imported may be different than thepackage.json
in the directory that mocha is being called, even in the event that the target file's path does not includenode_modules
.The text was updated successfully, but these errors were encountered: