☕️ Classic, reliable, trusted test framework for Node.js and the browser
chore(esm): remove code for Node versions <10 (#4736)
Juerg B97b84708afb4
diff --git a/.eslintrc.yml b/.eslintrc.ymlindex 3795fb90..d494481c 100644--- a/.eslintrc.yml+++ b/.eslintrc.yml@@ -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.jsdiff --git a/lib/cli/run-helpers.js b/lib/cli/run-helpers.jsindex d0f9e746..2be7529e 100644--- a/lib/cli/run-helpers.js+++ b/lib/cli/run-helpers.js@@ -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');/**diff --git a/lib/mocha.js b/lib/mocha.jsindex 42876adc..91e6f50a 100644--- a/lib/mocha.js+++ b/lib/mocha.js@@ -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,@@ -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) {diff --git a/lib/esm-utils.js b/lib/nodejs/esm-utils.jssimilarity index 100%rename from lib/esm-utils.jsrename to lib/nodejs/esm-utils.jsdiff --git a/lib/utils.js b/lib/utils.jsindex 91203475..e392c30d 100644--- a/lib/utils.js+++ b/lib/utils.js@@ -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*diff --git a/package.json b/package.jsonindex 48f7a115..e730bd56 100644--- a/package.json+++ b/package.json@@ -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": {diff --git a/test/integration/esm.spec.js b/test/integration/esm.spec.jsindex 7398af4e..8be983f9 100644--- a/test/integration/esm.spec.js+++ b/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) {@@ -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) {@@ -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'});diff --git a/test/integration/options/parallel.spec.js b/test/integration/options/parallel.spec.jsindex 8bd3729f..7c1a32cc 100644--- a/test/integration/options/parallel.spec.js+++ b/test/integration/options/parallel.spec.js@@ -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');@@ -108,19 +107,9 @@ 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]@@ -128,7 +117,6 @@ describe('--parallel', function() {const actual = getSummary(await invokeMochaAsync([- ...esmArgs,'--parallel',resolveFixturePath('esm/*.fixture.mjs')])[1]diff --git a/test/integration/plugins/root-hooks.spec.js b/test/integration/plugins/root-hooks.spec.jsindex 53d18d58..c70e8330 100644--- a/test/integration/plugins/root-hooks.spec.js+++ b/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@@ -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',@@ -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(@@ -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',diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.jsindex 6255e2ad..2275a2e5 100644--- a/test/unit/mocha.spec.js+++ b/test/unit/mocha.spec.js@@ -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');