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?