Skip to content

Commit

Permalink
chore(esm): remove code for Node versions <10 (#4736)
Browse files Browse the repository at this point in the history
and move 'lib/esm-utils.js' to 'lib/nodejs/esm-utils.js'
  • Loading branch information
juergba committed Sep 4, 2021
1 parent 654b5df commit 97b8470
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Expand Up @@ -35,7 +35,7 @@ overrides:
env:
browser: false
- files:
- esm-utils.js
- lib/nodejs/esm-utils.js
- rollup.config.js
- rollup_no-ie11.config.js
- scripts/pick-from-package-json.js
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/run-helpers.js
Expand Up @@ -14,7 +14,7 @@ const {watchRun, watchParallelRun} = require('./watch-run');
const collectFiles = require('./collect-files');
const {format} = require('util');
const {createInvalidLegacyPluginError} = require('../errors');
const {requireOrImport} = require('../esm-utils');
const {requireOrImport} = require('../nodejs/esm-utils');
const PluginLoader = require('../plugin-loader');

/**
Expand Down
10 changes: 1 addition & 9 deletions lib/mocha.js
Expand Up @@ -13,9 +13,7 @@ var growl = require('./nodejs/growl');
var utils = require('./utils');
var mocharc = require('./mocharc.json');
var Suite = require('./suite');
var esmUtils = utils.supportsEsModules(true)
? require('./esm-utils')
: undefined;
var esmUtils = require('./nodejs/esm-utils');
var createStatsCollector = require('./stats-collector');
const {
warn,
Expand Down Expand Up @@ -460,12 +458,6 @@ Mocha.prototype.loadFilesAsync = function() {
var suite = this.suite;
this.lazyLoadFiles(true);

if (!esmUtils) {
return new Promise(function(resolve) {
self.loadFiles(resolve);
});
}

return esmUtils.loadFilesAsync(
this.files,
function(file) {
Expand Down
File renamed without changes.
26 changes: 0 additions & 26 deletions lib/utils.js
Expand Up @@ -565,32 +565,6 @@ exports.defineConstants = function(obj) {
return Object.freeze(exports.createMap(obj));
};

/**
* Whether current version of Node support ES modules
*
* @description
* Versions prior to 10 did not support ES Modules, and version 10 has an old incompatible version of ESM.
* 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(partialSupport) {
if (!exports.isBrowser() && process.versions && process.versions.node) {
var versionFields = process.versions.node.split('.');
var major = +versionFields[0];
var minor = +versionFields[1];

if (!partialSupport) {
return major >= 13 || (major === 12 && minor >= 11);
} else {
return major >= 10;
}
}
};

/**
* Returns current working directory
*
Expand Down
10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -175,17 +175,17 @@
],
"browser": {
"./index.js": "./browser-entry.js",
"./lib/nodejs/growl.js": "./lib/browser/growl.js",
"./lib/esm-utils.js": false,
"./lib/nodejs/growl.js": "./lib/browser/growl.js",
"fs": false,
"path": false,
"supports-color": false,
"./lib/nodejs/serializer.js": false,
"./lib/nodejs/worker.js": false,
"./lib/nodejs/buffered-worker-pool.js": false,
"./lib/nodejs/esm-utils.js": false,
"./lib/nodejs/file-unloader.js": false,
"./lib/nodejs/parallel-buffered-runner.js": false,
"./lib/nodejs/serializer.js": false,
"./lib/nodejs/worker.js": false,
"./lib/nodejs/reporters/parallel-buffered.js": false,
"./lib/nodejs/file-unloader.js": false,
"./lib/cli/index.js": false
},
"prettier": {
Expand Down
16 changes: 2 additions & 14 deletions test/integration/esm.spec.js
@@ -1,15 +1,9 @@
'use strict';
var path = require('path');
const {runMochaJSON: run, runMochaAsync} = require('./helpers');
var utils = require('../../lib/utils');
var args =
+process.versions.node.split('.')[0] >= 13 ? [] : ['--experimental-modules'];
var args = [];

describe('esm', function() {
before(function() {
if (!utils.supportsEsModules(true)) this.skip();
});

it('should pass a passing esm test that uses esm', function(done) {
var fixture = 'esm/esm-success.fixture.mjs';
run(fixture, args, function(err, result) {
Expand Down Expand Up @@ -51,8 +45,6 @@ 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 All @@ -69,11 +61,7 @@ describe('esm', function() {
var fixture = 'esm/test-that-uses-dir-cjs-require.fixture.js';
const result = await runMochaAsync(
fixture,
[
...args,
'--require',
path.resolve(__dirname, './fixtures/esm/dir-cjs-require')
],
['--require', path.resolve(__dirname, './fixtures/esm/dir-cjs-require')],
{stdio: 'pipe'}
);

Expand Down
12 changes: 0 additions & 12 deletions test/integration/options/parallel.spec.js
Expand Up @@ -6,7 +6,6 @@ const {
getSummary,
resolveFixturePath
} = require('../helpers');
const utils = require('../../../lib/utils');
const pidtree = require('pidtree');

const REPORTER_FIXTURE_PATH = resolveFixturePath('options/parallel/test-a');
Expand Down Expand Up @@ -108,27 +107,16 @@ describe('--parallel', function() {
});

describe('when used with ESM tests', function() {
const esmArgs =
Number(process.versions.node.split('.')[0]) >= 13
? []
: ['--experimental-modules'];

before(function() {
if (!utils.supportsEsModules()) this.skip();
});

it('should have the same result as with --no-parallel', async function() {
const expected = getSummary(
await invokeMochaAsync([
...esmArgs,
'--no-parallel',
resolveFixturePath('esm/*.fixture.mjs')
])[1]
);

const actual = getSummary(
await invokeMochaAsync([
...esmArgs,
'--parallel',
resolveFixturePath('esm/*.fixture.mjs')
])[1]
Expand Down
57 changes: 19 additions & 38 deletions test/integration/plugins/root-hooks.spec.js
@@ -1,7 +1,6 @@
'use strict';

var invokeMochaAsync = require('../helpers').invokeMochaAsync;
var utils = require('../../../lib/utils');

/**
* Extracts root hook log messages from run results
Expand Down Expand Up @@ -99,38 +98,28 @@ describe('root hooks', function() {
});

describe('support ESM when type=module or .mjs extension', function() {
before(function() {
if (!utils.supportsEsModules()) this.skip();
});

it('should run root hooks when provided via mochaHooks', function() {
return expect(
runMochaForHookOutput(
[
'--require=' +
require.resolve(
// as object
'../fixtures/plugins/root-hooks/root-hook-defs-esm.fixture.mjs'
),
'--require=' +
require.resolve(
// as function
'../fixtures/plugins/root-hooks/esm/root-hook-defs-esm.fixture.js'
),
'--require=' +
require.resolve(
// mixed with commonjs
'../fixtures/plugins/root-hooks/root-hook-defs-a.fixture.js'
),
runMochaForHookOutput([
'--require=' +
require.resolve(
// as object
'../fixtures/plugins/root-hooks/root-hook-defs-esm.fixture.mjs'
),
'--require=' +
require.resolve(
'../fixtures/plugins/root-hooks/root-hook-test.fixture.js'
)
].concat(
+process.versions.node.split('.')[0] >= 13
? []
: '--experimental-modules'
// as function
'../fixtures/plugins/root-hooks/esm/root-hook-defs-esm.fixture.js'
),
'--require=' +
require.resolve(
// mixed with commonjs
'../fixtures/plugins/root-hooks/root-hook-defs-a.fixture.js'
),
require.resolve(
'../fixtures/plugins/root-hooks/root-hook-test.fixture.js'
)
),
]),
'to be fulfilled with',
[
'afterAll',
Expand All @@ -147,10 +136,6 @@ describe('root hooks', function() {
});

describe('support ESM via .js extension w/o type=module', function() {
before(function() {
if (!utils.supportsEsModules()) this.skip();
});

it('should fail due to ambiguous file type', function() {
return expect(
invokeMochaAsync(
Expand All @@ -160,11 +145,7 @@ describe('root hooks', function() {
// as object
'../fixtures/plugins/root-hooks/root-hook-defs-esm-broken.fixture.js'
)
].concat(
+process.versions.node.split('.')[0] >= 13
? []
: '--experimental-modules'
),
],
'pipe'
)[1],
'when fulfilled',
Expand Down
1 change: 0 additions & 1 deletion test/unit/mocha.spec.js
Expand Up @@ -78,7 +78,6 @@ describe('Mocha', function() {
Suite = sinon.stub(Mocha, 'Suite').returns(suite);
Suite.constants = {};

sinon.stub(utils, 'supportsEsModules').returns(false);
sinon.stub(errors, 'warn');
sinon.stub(utils, 'isString');
sinon.stub(utils, 'noop');
Expand Down

0 comments on commit 97b8470

Please sign in to comment.