Skip to content

Commit 3722201

Browse files
authored
Fix: using custom interface in parallel mode (#4688)
1 parent 6830821 commit 3722201

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

lib/cli/run-helpers.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ exports.runMocha = async (mocha, options) => {
195195
* it actually exists. This must be run _after_ requires are processed (see
196196
* {@link handleRequires}), as it'll prevent interfaces from loading otherwise.
197197
* @param {Object} opts - Options object
198-
* @param {"reporter"|"interface"} pluginType - Type of plugin.
199-
* @param {Object} [map] - An object perhaps having key `key`. Used as a cache
200-
* of sorts; `Mocha.reporters` is one, where each key corresponds to a reporter
201-
* name
198+
* @param {"reporter"|"ui"} pluginType - Type of plugin.
199+
* @param {Object} [map] - Used as a cache of sorts;
200+
* `Mocha.reporters` where each key corresponds to a reporter name,
201+
* `Mocha.interfaces` where each key corresponds to an interface name.
202202
* @private
203203
*/
204204
exports.validateLegacyPlugin = (opts, pluginType, map = {}) => {
@@ -226,12 +226,12 @@ exports.validateLegacyPlugin = (opts, pluginType, map = {}) => {
226226
// if this exists, then it's already loaded, so nothing more to do.
227227
if (!map[pluginId]) {
228228
try {
229-
opts[pluginType] = require(pluginId);
229+
map[pluginId] = require(pluginId);
230230
} catch (err) {
231231
if (err.code === 'MODULE_NOT_FOUND') {
232232
// Try to load reporters from a path (absolute or relative)
233233
try {
234-
opts[pluginType] = require(path.resolve(pluginId));
234+
map[pluginId] = require(path.resolve(pluginId));
235235
} catch (err) {
236236
throw createUnknownError(err);
237237
}

lib/errors.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ function createFatalError(message, value) {
328328
/**
329329
* Dynamically creates a plugin-type-specific error based on plugin type
330330
* @param {string} message - Error message
331-
* @param {"reporter"|"interface"} pluginType - Plugin type. Future: expand as needed
331+
* @param {"reporter"|"ui"} pluginType - Plugin type. Future: expand as needed
332332
* @param {string} [pluginId] - Name/path of plugin, if any
333333
* @throws When `pluginType` is not known
334334
* @public
@@ -339,7 +339,7 @@ function createInvalidLegacyPluginError(message, pluginType, pluginId) {
339339
switch (pluginType) {
340340
case 'reporter':
341341
return createInvalidReporterError(message, pluginId);
342-
case 'interface':
342+
case 'ui':
343343
return createInvalidInterfaceError(message, pluginId);
344344
default:
345345
throw new Error('unknown pluginType "' + pluginType + '"');

test/node-unit/cli/run-helpers.spec.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {validateLegacyPlugin, list} = require('../../../lib/cli/run-helpers');
4+
const Mocha = require('../../../lib/mocha');
45

56
describe('helpers', function() {
67
describe('validateLegacyPlugin()', function() {
@@ -25,24 +26,19 @@ describe('helpers', function() {
2526
});
2627
});
2728

28-
describe('when used with an "interfaces" key', function() {
29+
describe('when used with an "ui" key', function() {
2930
it('should disallow an array of names', function() {
30-
expect(
31-
() => validateLegacyPlugin({interface: ['bar']}, 'interface'),
32-
'to throw',
33-
{
34-
code: 'ERR_MOCHA_INVALID_INTERFACE',
35-
message: /can only be specified once/i
36-
}
37-
);
31+
expect(() => validateLegacyPlugin({ui: ['bar']}, 'ui'), 'to throw', {
32+
code: 'ERR_MOCHA_INVALID_INTERFACE',
33+
message: /can only be specified once/i
34+
});
3835
});
3936

4037
it('should fail to recognize an unknown interface', function() {
41-
expect(
42-
() => validateLegacyPlugin({interface: 'bar'}, 'interface'),
43-
'to throw',
44-
{code: 'ERR_MOCHA_INVALID_INTERFACE', message: /cannot find module/i}
45-
);
38+
expect(() => validateLegacyPlugin({ui: 'bar'}, 'ui'), 'to throw', {
39+
code: 'ERR_MOCHA_INVALID_INTERFACE',
40+
message: /cannot find module/i
41+
});
4642
});
4743
});
4844

@@ -56,6 +52,17 @@ describe('helpers', function() {
5652
});
5753
});
5854

55+
describe('when used with a third-party interface', function() {
56+
it('should add the interface to "Mocha.interfaces"', function() {
57+
// let's suppose that `glob` is an interface
58+
const opts = {ui: 'glob'};
59+
validateLegacyPlugin(opts, 'ui', Mocha.interfaces);
60+
expect(opts.ui, 'to equal', 'glob');
61+
expect(Mocha.interfaces, 'to satisfy', {glob: require('glob')});
62+
delete Mocha.interfaces.glob;
63+
});
64+
});
65+
5966
describe('when a plugin throws an exception upon load', function() {
6067
it('should fail and report the original error', function() {
6168
expect(

0 commit comments

Comments
 (0)