From 0e34c121b32b875a6b2c93202c2e9edefdc942d2 Mon Sep 17 00:00:00 2001 From: Gil Tayar Date: Fri, 22 May 2020 10:42:06 +0300 Subject: [PATCH] enable esm support in Node v10 --- docs/index.md | 1 + lib/mocha.js | 4 +++- lib/utils.js | 10 +++++++--- test/integration/esm.spec.js | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/index.md b/docs/index.md index 7f44f34acc..56cee11220 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1443,6 +1443,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 1ab26958c8..b909040112 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(false) + ? 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 0134247022..d9f3e343cd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -809,16 +809,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 {fullSpecification} 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(fullSpecification) { if (!process.browser && 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 (fullSpecification) { + 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..ac0386260a 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) {