Skip to content
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

If one among multiple ESM tests has a syntax error, then Mocha doesn’t report which one #4551

Closed
4 tasks done
rauschma opened this issue Jan 12, 2021 · 7 comments
Closed
4 tasks done
Labels
area: node.js command-line-or-Node.js-specific area: usability concerning user experience or interface type: bug a defect, confirmed by a maintainer

Comments

@rauschma
Copy link

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

If I run multiple ESM tests (e.g. via npm t test*.mjs), and one of them has a syntax error, then Mocha doesn’t tell me which file it is.

Steps to Reproduce

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’d like Mocha to tell me which file has the syntax error.

Actual behavior: I’m 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%

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 8.2.1 (there is no globally installed Mocha)
  • The output of node --version: v14.15.4
@outsideris outsideris added type: bug a defect, confirmed by a maintainer area: usability concerning user experience or interface and removed unconfirmed-bug labels Jan 19, 2021
@outsideris
Copy link
Member

outsideris commented Jan 19, 2021

It is current Node.js's behavior, actually V8 because we are using dynamic import. See nodejs/node#49441.
I understand how annoying us with this problem.
I'm not sure we should fix it on mocha side.

@outsideris outsideris added the area: node.js command-line-or-Node.js-specific label Jan 19, 2021
@rauschma
Copy link
Author

Oh wow. I agree!

@rauschma
Copy link
Author

rauschma commented Jan 19, 2021

On second thought: Even just showing the name of the file would already help a lot. That’s something that could be fixed on Mocha’s side, right?

@giltayar
Copy link
Contributor

@rauschma yup. I'll look into it

@rauschma
Copy link
Author

(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.)

@outsideris
Copy link
Member

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.

@juergba
Copy link
Member

juergba commented Feb 3, 2021

closed by #4557

@juergba juergba closed this as completed Feb 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: node.js command-line-or-Node.js-specific area: usability concerning user experience or interface type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants