mocha: Possible Bug with mocha new configuration file

Hi, I need help with an issue with mocha new configuration file. I apologize if I am going through the wrong avenue for help. Anyways my .mocharc.js file is in the root directory of my project and its content is

'use strict';

module.exports = {
  require: ['ts-node/register', 'esm'],
  file: ['./test/testSetup.ts'],
  reporter: 'nyan',
  ui: 'bdd',
};

when i run the cli mocha "./test/**/*.spec.tsx" I get an unexpected token export in one of my node_modules dependencies

when I specify explicitly through the cli mocha -r ts-node/register -r esm --file "./test/testSetup.ts" --ui bdd "./test/**/*.spec.tsx" it is able to run the test just fine

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 27 (14 by maintainers)

Most upvoted comments

Hi,

I had a similar issue to this happen to me earlier today.

Using version 6.1.4 I was able to use mocha with esm with the following sections in my package.json file:

"scripts": {
  "test": "mocha"
},
"mocha": {
  "require": [
    "esm"
  ],
  "recursive": true
}

Post upgrade to mocha 6.2.0 this did not work. The esm module wasn’t being called. I was getting the dreaded SyntaxError: Unexpected token { error for my import statement.

I needed to change my package.json file so that esm was explicitly called via the command line. For example:

"scripts": {
  "test": "mocha --require esm"
},
"mocha": {
  "require": [
    "esm"
  ],
  "recursive": true
}

I’m not sure if these two issues are related or not. As I found this thread via a Google search to work out what was wrong, I thought it worth mentioning.

@juergba I can confirm that with the patch applied as per #3983 my use case works. Specifically using the following in my package.json file.

"scripts": {
  "test": "mocha"
},
"mocha": {
  "require": [
    "esm"
  ],
  "recursive": true
}

My thanks to you and the team for working on this issue.

@juergba patching bin/mocha with that solution works. Thank you!

@boneskull thank you for your input.

Can I propose an alternative, softer revert. We spawn a child-process if:

  • either Node flags
  • or --require esm option (via CLI or RC file)

In bin/mocha: if (Object.keys(nodeArgs).length || mochaArgs.require && mochaArgs.require.includes('esm')) {

EDIT: --require is also a node flag, so we could put --require esm directly into nodeArgs.