Skip to content

Commit

Permalink
enable esm support only above 12.11.0 due to bug in Node ESM impl
Browse files Browse the repository at this point in the history
Node 12.11.0 has a bug in dealing with dynamic import and type="module"
So I decided to suppress ESM support in Mocha prior to that version, which fixes
the bug
  • Loading branch information
giltayar committed Oct 1, 2019
1 parent 9f38e09 commit f9f92f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ this means either ending the file with a `.mjs` extension, or, if you want to us
adding `"type": "module"` to your `package.json`.
More information can be found in the [Node.js documentation](https://nodejs.org/api/esm.html).

> Mocha supports ES modules only from Node.js v12 and above. Also note that
> Mocha supports ES modules only from Node.js v12.11.0 and above. Also note that
> to enable this, you need to add `--experimental-modules` when running
> Mocha. Last, but not least, the ESM implementation Mocha is not yet
> stable so given that the specification in Node.js may
Expand Down
20 changes: 17 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,21 @@ exports.defineConstants = function(obj) {
* @returns {Boolean} whether the current version of Node.JS supports ES Modules in a way that is compatbile with Mocha
*/
exports.supportsEsModules = function() {
return typeof document === 'undefined'
? +process.versions.node.split('.')[0] >= 12
: false;
if (typeof document !== 'undefined') {
return false;
}

var versionFields = process.versions.node.split('.');
var major = +versionFields[0];

if (major >= 13) {
return true;
}
if (major < 12) {
return false;
}
// major === 12
var minor = +versionFields[1];

return minor >= 11;
};

0 comments on commit f9f92f8

Please sign in to comment.