Mocha: If one among multiple ESM tests has a syntax error, then Mocha doesn鈥檛 report which one

Created on 12 Jan 2021  路  6Comments  路  Source: mochajs/mocha

Prerequisites

  • [x] Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • [x] 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.
  • [x] '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
  • [x] 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鈥檛 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鈥檇 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%

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
confirmed-bug node.js usability

Most helpful comment

@rauschma yup. I'll look into it

All 6 comments

It 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CADBOT picture CADBOT  路  3Comments

enigmatic00 picture enigmatic00  路  3Comments

Aarbel picture Aarbel  路  3Comments

wzup picture wzup  路  3Comments

luoxi001713 picture luoxi001713  路  3Comments