diff --git a/docs/index.md b/docs/index.md index 1fa8ada2ec..29c02b4061 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1812,6 +1812,7 @@ More information can be found in the [Node.js documentation](https://nodejs.org/ > Mocha supports ES modules only from Node.js v12.11.0 and above. To enable this in versions smaller than 13.2.0, you need to add `--experimental-modules` when running > Mocha. From version 13.2.0 of Node.js, you can use ES modules without any flags. +> (Mocha _will_ load ESM even in Node v10, but this is not officially supported. Use at your own risk.) ### Current Limitations diff --git a/lib/mocha.js b/lib/mocha.js index 5c39d62cac..8aabcb8762 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -14,7 +14,9 @@ var utils = require('./utils'); var mocharc = require('./mocharc.json'); var errors = require('./errors'); var Suite = require('./suite'); -var esmUtils = utils.supportsEsModules() ? require('./esm-utils') : undefined; +var esmUtils = utils.supportsEsModules(true) + ? require('./esm-utils') + : undefined; var createStatsCollector = require('./stats-collector'); var createInvalidReporterError = errors.createInvalidReporterError; var createInvalidInterfaceError = errors.createInvalidInterfaceError; diff --git a/lib/utils.js b/lib/utils.js index b338bc094c..e09322b9ee 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -808,16 +808,20 @@ exports.defineConstants = function(obj) { * This function returns whether Node.JS has ES Module supports that is compatible with Mocha's needs, * which is version >=12.11. * + * @param {partialSupport} whether the full Node.js ESM support is available (>= 12) or just something that supports the runtime (>= 10) + * * @returns {Boolean} whether the current version of Node.JS supports ES Modules in a way that is compatible with Mocha */ -exports.supportsEsModules = function() { +exports.supportsEsModules = function(partialSupport) { if (!exports.isBrowser() && process.versions && process.versions.node) { var versionFields = process.versions.node.split('.'); var major = +versionFields[0]; var minor = +versionFields[1]; - if (major >= 13 || (major === 12 && minor >= 11)) { - return true; + if (!partialSupport) { + return major >= 13 || (major === 12 && minor >= 11); + } else { + return major >= 10; } } }; diff --git a/test/integration/esm.spec.js b/test/integration/esm.spec.js index b4cf761f2a..20b3a27248 100644 --- a/test/integration/esm.spec.js +++ b/test/integration/esm.spec.js @@ -6,7 +6,7 @@ var args = describe('esm', function() { before(function() { - if (!utils.supportsEsModules()) this.skip(); + if (!utils.supportsEsModules(true)) this.skip(); }); it('should pass a passing esm test that uses esm', function(done) { @@ -39,6 +39,8 @@ describe('esm', function() { }); it('should recognize esm files ending with .js due to package.json type flag', function(done) { + if (!utils.supportsEsModules(false)) return this.skip(); + var fixture = 'esm/js-folder/esm-in-js.fixture.js'; run(fixture, args, function(err, result) { if (err) {