Skip to content

Commit

Permalink
enable esm support in Node v10
Browse files Browse the repository at this point in the history
  • Loading branch information
giltayar committed May 22, 2020
1 parent cb5eb8e commit 0e34c12
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/mocha.js
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions lib/utils.js
Expand Up @@ -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;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion test/integration/esm.spec.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 0e34c12

Please sign in to comment.