mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.52k stars 3k forks source link

🐛 Bug: Error loading 3rd-party reporters swallowed and replaced with generic "invalid reporter" #2575

Open yuriyKazan opened 7 years ago

yuriyKazan commented 7 years ago

SR: 1) Use the npm to install third-party reporter (e.g. myReporter). 2) Modify the reporter by adding a module (e.g. "diff = require('diff')") into myReporter.js file. 3) Try to run the mocha with the report.

ER: Appears correct error message (e.g. Error: Cannot find module 'diff'). AR: Appears error message:

node node_modules/mocha/bin/mocha test.js --reporter myReporter
"myReporter" reporter not found
\node_modules\mocha\lib\mocha.js:166
      throw new Error('invalid reporter "' + reporter + '"');
      ^

Error: invalid reporter "myReporter"
    at Mocha.reporter (\node_modules\mocha\lib\mocha.js:166:13)
    at Object.<anonymous> (\node_modules\mocha\bin\_mocha:212:7)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

I reported the issue here in case if someone also will encounter the problem.

boneskull commented 7 years ago

yeah this could be better

ScottFreeCode commented 7 years ago

We should probably just replace require in all the try-catches with require.resolve and do the actual require outside of try-catch, so we can still give an error if all the require.resolves fail (could update from 'invalid reporter <reporter>' to 'no reporter "<reporter>" found') but if the reporter is there but not loadable the original error is still thrown.