plaviDev
plaviDev

Reputation: 111

Mocha with --watch flag set throws an error when using ES6 modules

I've been trying to set up Mocha to automatically watch for changes in my files by using the --watch flag. I've set up two scripts in package.json like so:

  "test": "mocha",
  "test:watch": "mocha --watch ./test ./game_logic" 

When invoking the first script with npm test the tests are performed without an error, however when I invoke the second script with npm run test:watch, the following error is thrown:

 (node:149779) UnhandledPromiseRejectionWarning: Error [ERR_REQUIRE_ESM]: Must use import to load 
 ES Module: <path to test file contained in /test>

I don't quite understand the error message because I'm already using import whenever I'm importing whole files or separate functionalities from one file into another.

The documentation from Mocha states that writing test as ES modules is supported so I really don't have a clue why the described error occurs.

https://mochajs.org/#nodejs-native-esm-support

Upvotes: 11

Views: 929

Answers (1)

Rich Apodaca
Rich Apodaca

Reputation: 29014

As per this issue, a workaround is to add the --parallel flag, like so:

mocha --watch --parallel --recursive test

Upvotes: 8

Related Questions