Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

fix: load mjs files correctly (#5429)

Mark Wiemera947b9b95501
diff --git a/eslint.config.js b/eslint.config.js
index e52e18f3..93aec09a 100644
--- a/eslint.config.js
+++ b/eslint.config.js
@@ -71,7 +71,7 @@ module.exports = [
},
{
files: [
- 'test/compiler-esm/*.js'
+ 'test/compiler-esm/*.{js,mjs}'
],
languageOptions: {
sourceType: 'module',
diff --git a/lib/nodejs/esm-utils.js b/lib/nodejs/esm-utils.js
index 3cea8af2..620fb7e7 100644
--- a/lib/nodejs/esm-utils.js
+++ b/lib/nodejs/esm-utils.js
@@ -90,14 +90,19 @@ const tryImportAndRequire = async (file, esmDecorator) => {
// and `require.cache` effective, while allowing us to load ESM modules
// and CJS modules in the same way.
const requireModule = async (file, esmDecorator) => {
+ if (path.extname(file) === '.mjs') {
+ return formattedImport(file, esmDecorator);
+ }
try {
return require(file);
} catch (err) {
// Import if require fails.
return dealWithExports(await formattedImport(file, esmDecorator));
}
-}
+};
+// We only assign this `requireOrImport` function once based on Node version
+// We check for file extensions in `requireModule` and `tryImportAndRequire`
if (process.features.require_module) {
exports.requireOrImport = requireModule;
} else {
diff --git a/test/compiler-esm/test-tla.mjs b/test/compiler-esm/test-tla.mjs
new file mode 100644
index 00000000..55705485
--- /dev/null
+++ b/test/compiler-esm/test-tla.mjs
@@ -0,0 +1,11 @@
+const obj = { foo: 'bar' };
+
+describe('esm written in esm with top-level-await', () => {
+ it('should work', () => {
+ expect(obj, 'to equal', { foo: 'bar' });
+ });
+});
+
+await undefined;
+
+export const foo = 'bar';
diff --git a/test/compiler-esm/test-tla.mjs.compiled b/test/compiler-esm/test-tla.mjs.compiled
new file mode 100644
index 00000000..55705485
--- /dev/null
+++ b/test/compiler-esm/test-tla.mjs.compiled
@@ -0,0 +1,11 @@
+const obj = { foo: 'bar' };
+
+describe('esm written in esm with top-level-await', () => {
+ it('should work', () => {
+ expect(obj, 'to equal', { foo: 'bar' });
+ });
+});
+
+await undefined;
+
+export const foo = 'bar';
diff --git a/test/compiler-esm/test.mjs b/test/compiler-esm/test.mjs
new file mode 100644
index 00000000..0541b4c9
--- /dev/null
+++ b/test/compiler-esm/test.mjs
@@ -0,0 +1,9 @@
+const obj = {foo: 'bar'};
+
+describe('esm written in esm', () => {
+ it('should work', () => {
+ expect(obj, 'to equal', {foo: 'bar'});
+ });
+});
+
+export const foo = 'bar';
diff --git a/test/compiler-esm/test.mjs.compiled b/test/compiler-esm/test.mjs.compiled
new file mode 100644
index 00000000..0541b4c9
--- /dev/null
+++ b/test/compiler-esm/test.mjs.compiled
@@ -0,0 +1,9 @@
+const obj = {foo: 'bar'};
+
+describe('esm written in esm', () => {
+ it('should work', () => {
+ expect(obj, 'to equal', {foo: 'bar'});
+ });
+});
+
+export const foo = 'bar';
diff --git a/test/integration/compiler-esm-only-loader.spec.js b/test/integration/compiler-esm-only-loader.spec.js
index c1c1e49f..c2b222cf 100644
--- a/test/integration/compiler-esm-only-loader.spec.js
+++ b/test/integration/compiler-esm-only-loader.spec.js
@@ -5,13 +5,18 @@ var path = require('node:path');
// Regression test for https://github.com/mochajs/mocha/issues/5380
describe('support ESM only module loader packages', function () {
- it('should support ESM .js extension', function (done) {
+ /**
+ * Load the filename using an ESM-only loader and expect things to work
+ * @param {string} filename Name of the file to load
+ * @param {Function} done A Mocha done callback for when the test ends
+ */
+ function loadAndExpect(filename, done) {
exec(
'"' +
process.execPath +
'" "' +
path.join('bin', 'mocha') +
- '" -R json --require "@test/esm-only-loader" "test/compiler-esm/*.js"',
+ `" -R json --require "@test/esm-only-loader" "${filename}"`,
{cwd: path.join(__dirname, '..', '..')},
function (error, stdout) {
if (error && !stdout) {
@@ -24,48 +29,26 @@ describe('support ESM only module loader packages', function () {
expect(results.tests[index], 'to have property', 'fullTitle');
titles.push(results.tests[index].fullTitle);
}
- expect(
- titles,
- 'to contain',
- 'esm written in esm should work',
- ).and(
- 'to contain',
- 'esm written in esm with top-level-await should work',
- ).and('to have length', 2);
+ expect(titles, 'to contain', 'esm written in esm should work')
+ .and(
+ 'to contain',
+ 'esm written in esm with top-level-await should work'
+ )
+ .and('to have length', 2);
done();
}
);
+ }
+
+ it('should support ESM .js extension', function (done) {
+ loadAndExpect('test/compiler-esm/*.js', done);
});
it('should support ESM .ts extension', function (done) {
- exec(
- '"' +
- process.execPath +
- '" "' +
- path.join('bin', 'mocha') +
- '" -R json --require "@test/esm-only-loader" "test/compiler-esm/*.ts"',
- {cwd: path.join(__dirname, '..', '..')},
- function (error, stdout) {
- if (error && !stdout) {
- return done(error);
- }
- var results = JSON.parse(stdout);
- expect(results, 'to have property', 'tests');
- var titles = [];
- for (var index = 0; index < results.tests.length; index += 1) {
- expect(results.tests[index], 'to have property', 'fullTitle');
- titles.push(results.tests[index].fullTitle);
- }
- expect(
- titles,
- 'to contain',
- 'esm written in esm should work',
- ).and(
- 'to contain',
- 'esm written in esm with top-level-await should work',
- ).and('to have length', 2);
- done();
- }
- );
+ loadAndExpect('test/compiler-esm/*.ts', done);
+ });
+
+ it('should support ESM .mjs extension', function (done) {
+ loadAndExpect('test/compiler-esm/*.mjs', done);
});
});
diff --git a/test/integration/compiler-esm.spec.js b/test/integration/compiler-esm.spec.js
index 10a486a7..8aca1ff2 100644
--- a/test/integration/compiler-esm.spec.js
+++ b/test/integration/compiler-esm.spec.js
@@ -4,13 +4,18 @@ var exec = require('node:child_process').exec;
var path = require('node:path');
describe('support ESM module loader compilers', function () {
- it('should support ESM .js extension', function (done) {
+ /**
+ * Runs `exec` on an ESM setup for the given file
+ * @param {string} filename Path to the file to load
+ * @param {Function} done A Mocha done callback for when the test ends
+ */
+ function loadAndExpect(filename, done) {
exec(
'"' +
process.execPath +
'" "' +
path.join('bin', 'mocha') +
- '" -R json --require test/compiler-fixtures/esm.fixture "test/compiler-esm/*.js"',
+ `" -R json --require test/compiler-fixtures/esm.fixture "${filename}"`,
{cwd: path.join(__dirname, '..', '..')},
function (error, stdout) {
if (error && !stdout) {
@@ -23,48 +28,26 @@ describe('support ESM module loader compilers', function () {
expect(results.tests[index], 'to have property', 'fullTitle');
titles.push(results.tests[index].fullTitle);
}
- expect(
- titles,
- 'to contain',
- 'esm written in esm should work',
- ).and(
- 'to contain',
- 'esm written in esm with top-level-await should work',
- ).and('to have length', 2);
+ expect(titles, 'to contain', 'esm written in esm should work')
+ .and(
+ 'to contain',
+ 'esm written in esm with top-level-await should work'
+ )
+ .and('to have length', 2);
done();
}
);
+ }
+
+ it('should support ESM .js extension', function (done) {
+ loadAndExpect('test/compiler-esm/*.js', done);
});
it('should support ESM .ts extension', function (done) {
- exec(
- '"' +
- process.execPath +
- '" "' +
- path.join('bin', 'mocha') +
- '" -R json --require test/compiler-fixtures/esm.fixture "test/compiler-esm/*.ts"',
- {cwd: path.join(__dirname, '..', '..')},
- function (error, stdout) {
- if (error && !stdout) {
- return done(error);
- }
- var results = JSON.parse(stdout);
- expect(results, 'to have property', 'tests');
- var titles = [];
- for (var index = 0; index < results.tests.length; index += 1) {
- expect(results.tests[index], 'to have property', 'fullTitle');
- titles.push(results.tests[index].fullTitle);
- }
- expect(
- titles,
- 'to contain',
- 'esm written in esm should work',
- ).and(
- 'to contain',
- 'esm written in esm with top-level-await should work',
- ).and('to have length', 2);
- done();
- }
- );
+ loadAndExpect('test/compiler-esm/*.ts', done);
+ });
+
+ it('should support ESM .mjs extension', function (done) {
+ loadAndExpect('test/compiler-esm/*.mjs', done);
});
});