Fix: wrong error thrown while loading reporter#4842
Conversation
| let foundReporter; | ||
| try { | ||
| reporter = require(reporterName); | ||
| foundReporter = require.resolve(reporterName); |
There was a problem hiding this comment.
@juergba this line caused a warning when trying to bundle mocha/mocha.js using webpack
There was a problem hiding this comment.
@AviVahl This one: Critical dependency: the request of a dependency is an expression?
Is this a problem? I need to check wether the module exists, and I can't use a fs function either.
In a browser environment we shouldn't get up to this line anyway.
There was a problem hiding this comment.
Yes, exactly that one.
The warning actually broke several internal projects we have that bundle mocha into the browser. There was infra code there that verified no errors/warnings from bundling.
If the branch is node-specific, would it be possible to completely separate it out of the browser bundle?
There was a problem hiding this comment.
How can I achieve this, exclude a specific code block from the bundle?
Btw the transpiled ES5 bundle (mocha.js) will be dropped in a few weeks, and the ES2018 bundle (mocha-es2018.js renamed to mocha.js) will be the one and only. If you have any input, would be welcome.
There was a problem hiding this comment.
I can think of several possible ways to achieve this.
I usually separate the isomorphic parts of code from the Node-specific parts.
So: the cli, config loading, reporter loading, dynamic require/require.resolve, etc would all belong to the Node-specific part.
The mocha.js bundle would only include the runner itself and associated APIs.
Another, perhaps trickier, way to achieve this is to specify "browser": { "./my-node-specific-file.js": null} which would cause bundlers to skip that file and inject an empty object instead. Runtime checks would be needed to protect against the replaced API.
Regarding es2018? My team welcomes this change. :)
There was a problem hiding this comment.
This issue is present even now in v10. Have you found a solution @AviVahl ? I can supress these errors in webpack config but it doesn't seem like the right solution.
There was a problem hiding this comment.
We've stopped bundling mocha from our end because of the many bundling regressions it exhibited across the years/versions. Our tool (mocha-play) uses the prebuilt bundle (in the npm package) and injects it to the page.
Description
When developing a third-party reporter, Mocha misdiagnoses an error coming from within the reporter as the reporter module not being found.
Description of the Change
reporter()when used programmatically via Mocha APIApplicable issues
closes #3596