☕️ Classic, reliable, trusted test framework for Node.js and the browser
Fix: using custom interface in parallel mode (#4688)
Juerg B37222019348d
diff --git a/lib/cli/run-helpers.js b/lib/cli/run-helpers.jsindex dfac8b44..d0f9e746 100644--- a/lib/cli/run-helpers.js+++ b/lib/cli/run-helpers.js@@ -195,10 +195,10 @@ exports.runMocha = async (mocha, options) => {* it actually exists. This must be run _after_ requires are processed (see* {@link handleRequires}), as it'll prevent interfaces from loading otherwise.* @param {Object} opts - Options object- * @param {"reporter"|"interface"} pluginType - Type of plugin.- * @param {Object} [map] - An object perhaps having key `key`. Used as a cache- * of sorts; `Mocha.reporters` is one, where each key corresponds to a reporter- * name+ * @param {"reporter"|"ui"} pluginType - Type of plugin.+ * @param {Object} [map] - Used as a cache of sorts;+ * `Mocha.reporters` where each key corresponds to a reporter name,+ * `Mocha.interfaces` where each key corresponds to an interface name.* @private*/exports.validateLegacyPlugin = (opts, pluginType, map = {}) => {@@ -226,12 +226,12 @@ exports.validateLegacyPlugin = (opts, pluginType, map = {}) => {// if this exists, then it's already loaded, so nothing more to do.if (!map[pluginId]) {try {- opts[pluginType] = require(pluginId);+ map[pluginId] = require(pluginId);} catch (err) {if (err.code === 'MODULE_NOT_FOUND') {// Try to load reporters from a path (absolute or relative)try {- opts[pluginType] = require(path.resolve(pluginId));+ map[pluginId] = require(path.resolve(pluginId));} catch (err) {throw createUnknownError(err);}diff --git a/lib/errors.js b/lib/errors.jsindex 681198ae..0f0940b4 100644--- a/lib/errors.js+++ b/lib/errors.js@@ -328,7 +328,7 @@ function createFatalError(message, value) {/*** Dynamically creates a plugin-type-specific error based on plugin type* @param {string} message - Error message- * @param {"reporter"|"interface"} pluginType - Plugin type. Future: expand as needed+ * @param {"reporter"|"ui"} pluginType - Plugin type. Future: expand as needed* @param {string} [pluginId] - Name/path of plugin, if any* @throws When `pluginType` is not known* @public@@ -339,7 +339,7 @@ function createInvalidLegacyPluginError(message, pluginType, pluginId) {switch (pluginType) {case 'reporter':return createInvalidReporterError(message, pluginId);- case 'interface':+ case 'ui':return createInvalidInterfaceError(message, pluginId);default:throw new Error('unknown pluginType "' + pluginType + '"');diff --git a/test/node-unit/cli/run-helpers.spec.js b/test/node-unit/cli/run-helpers.spec.jsindex e12b97b8..9c3f60ee 100644--- a/test/node-unit/cli/run-helpers.spec.js+++ b/test/node-unit/cli/run-helpers.spec.js@@ -1,6 +1,7 @@'use strict';const {validateLegacyPlugin, list} = require('../../../lib/cli/run-helpers');+const Mocha = require('../../../lib/mocha');describe('helpers', function() {describe('validateLegacyPlugin()', function() {@@ -25,24 +26,19 @@ describe('helpers', function() {});});- describe('when used with an "interfaces" key', function() {+ describe('when used with an "ui" key', function() {it('should disallow an array of names', function() {- expect(- () => validateLegacyPlugin({interface: ['bar']}, 'interface'),- 'to throw',- {- code: 'ERR_MOCHA_INVALID_INTERFACE',- message: /can only be specified once/i- }- );+ expect(() => validateLegacyPlugin({ui: ['bar']}, 'ui'), 'to throw', {+ code: 'ERR_MOCHA_INVALID_INTERFACE',+ message: /can only be specified once/i+ });});it('should fail to recognize an unknown interface', function() {- expect(- () => validateLegacyPlugin({interface: 'bar'}, 'interface'),- 'to throw',- {code: 'ERR_MOCHA_INVALID_INTERFACE', message: /cannot find module/i}- );+ expect(() => validateLegacyPlugin({ui: 'bar'}, 'ui'), 'to throw', {+ code: 'ERR_MOCHA_INVALID_INTERFACE',+ message: /cannot find module/i+ });});});@@ -56,6 +52,17 @@ describe('helpers', function() {});});+ describe('when used with a third-party interface', function() {+ it('should add the interface to "Mocha.interfaces"', function() {+ // let's suppose that `glob` is an interface+ const opts = {ui: 'glob'};+ validateLegacyPlugin(opts, 'ui', Mocha.interfaces);+ expect(opts.ui, 'to equal', 'glob');+ expect(Mocha.interfaces, 'to satisfy', {glob: require('glob')});+ delete Mocha.interfaces.glob;+ });+ });+describe('when a plugin throws an exception upon load', function() {it('should fail and report the original error', function() {expect(