faq labelnode node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you _not_ install Mocha globally.If I run multiple ESM tests (e.g. via npm t test*.mjs), and one of them has a syntax error, then Mocha doesn鈥檛 tell me which file it is.
Repository with MCVE: https://github.com/rauschma/mocha-test-repo
Execute either of the following two commands:
npm t test*.mjs
npm t 'test*.mjs'
test2.mjs has a syntax error.
Expected behavior: I鈥檇 like Mocha to tell me which file has the syntax error.
Actual behavior: I鈥檓 getting the following error message.
SyntaxError: Unexpected token '=>'
at Loader.moduleStrategy (internal/modules/esm/translators.js:117:18)
at async link (internal/modules/esm/module_job.js:42:21)
Reproduces how often: 100%
mocha --version and node node_modules/.bin/mocha --version: 8.2.1 (there is no globally installed Mocha)node --version: v14.15.4It is current Node.js's behavior, actually V8 because we are using dynamic import. See https://github.com/nodejs/modules/issues/471.
I understand how annoying us with this problem.
I'm not sure we should fix it on mocha side.
Oh wow. I agree!
On second thought: Even just showing the name of the file would already help a lot. That鈥檚 something that could be fixed on Mocha鈥檚 side, right?
@rauschma yup. I'll look into it
(Still not ideal, because the syntax error might be in a module imported by the original file or even further down the import tree. But it would be a welcome improvement until this issue is fixed in V8.)
When mocha run tests with CJS, it report SyntaxError with filename, linenumber and actual broken syntax part like:
/Users/username/mcve/test2.js:5
test('test2a', => { // syntax error!
^^
SyntaxError: Unexpected token '=>'
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.exports.requireOrImport (/Users/username/mcve/lib/esm-utils.js:29:12)
at Object.exports.loadFilesAsync (/Users/username/mcve/lib/esm-utils.js:42:34)
at Mocha.loadFilesAsync (/Users/username/mcve/lib/mocha.js:431:19)
at singleRun (/Users/username/mcve/lib/cli/run-helpers.js:125:15)
at exports.runMocha (/Users/username/mcve/lib/cli/run-helpers.js:190:10)
at Object.exports.handler (/Users/username/mcve/lib/cli/run.js:362:11)
at /Users/username/mcve/node_modules/yargs/lib/command.js:241:49
With dynamic import, we only know the filename.
I didn't find how we show the line number and the broken part yet.
Most helpful comment
@rauschma yup. I'll look into it