Skip to content

Commit

Permalink
enable esm support in Node v10 (#4299)
Browse files Browse the repository at this point in the history
* enable esm support in Node v10

* also test Node v10 esm support

* supportsEsModule default is now to check support for full (>=v12) specification

* restoring package-lock.json to the version with https
  • Loading branch information
giltayar authored Jun 5, 2020
1 parent ca1cfc4 commit a2f2e08
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/mocha.js
Original file line number Diff line number Diff line change
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(true)
? 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
};
Expand Down
4 changes: 3 additions & 1 deletion test/integration/esm.spec.js
Original file line number Diff line number Diff line change
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 Expand Up @@ -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) {
Expand Down

0 comments on commit a2f2e08

Please sign in to comment.