3

With commonjs and using require() for .js files and a command of

mocha `**/*.spec.js` 

Files:

capitalize.js
test/capitalize.js

my tests run

Output:

mocha
Capitalize sentences
✓ calls the function
✓ calls the function
✓ calls the function
✓ Works for a second sentence
✓ Handles 3 dots
✓ Handles existing capitalization dots
✓ Preserves existing last dot
✓ Preserves carriage returns ("\n")

I want to use ES6 modules. So I:

  • changed both file extensions to `.js from .mjs
  • changed file I an running (the test) from

    export function capitalize(paragraph) {
    

    to

    exports.capitalize = function(paragraph) {
    
  • changed file I include from

    export function capitalize(paragraph) { 
    

    to

    exports.capitalize = function(paragraph) { 
    

However, mocha isn't recognizing my tests and I get

Error: No test files found: "**/*.spec.js"

// Not too surprising given I changed the file extensions

If I now changed package.json from

"test": "mocha **/*.spec.js"

to

"test": "mocha **/*.spec.mjs"

I get

$ mocha Error: No test files found

from mocha directly, or with npm test I get

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:

How to fix?

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • How do you import other modules into your project? – Mirodil Jan 21 '20 at 19:13
  • Which version of mocha are you using? Is it [v7.0.0-esm1](https://github.com/mochajs/mocha/releases/tag/v7.0.0-esm1). as mentioned in [my comment](https://stackoverflow.com/questions/59803401/npm-test-must-use-import-to-load-es-module-but-i-thought-i-was-node-12#comment-105759358) to your other question which shows a same error. In issue [#3006](https://github.com/mochajs/mocha/issues/3006) it seems that ESM modules are not supported in mocha stable releases/versions - only supported in _v7.0.0-esm (experimental)_ release. – RobC Jan 24 '20 at 13:54

0 Answers0