Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable esm support in Node v10 #4299

Merged
merged 4 commits into from Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.md
Expand Up @@ -1785,6 +1785,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(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
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
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