Skip to content

Commit 241964b

Browse files
authored
fix: wrong error thrown while loading reporter (#4842)
1 parent 22f9306 commit 241964b

File tree

5 files changed

+43
-62
lines changed

5 files changed

+43
-62
lines changed

lib/cli/run-helpers.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,18 @@ exports.validateLegacyPlugin = (opts, pluginType, map = {}) => {
225225

226226
// if this exists, then it's already loaded, so nothing more to do.
227227
if (!map[pluginId]) {
228+
let foundId;
228229
try {
229-
map[pluginId] = require(pluginId);
230+
foundId = require.resolve(pluginId);
231+
map[pluginId] = require(foundId);
230232
} catch (err) {
231-
if (err.code === 'MODULE_NOT_FOUND') {
232-
// Try to load reporters from a path (absolute or relative)
233-
try {
234-
map[pluginId] = require(path.resolve(pluginId));
235-
} catch (err) {
236-
throw createUnknownError(err);
237-
}
238-
} else {
239-
throw createUnknownError(err);
233+
if (foundId) throw createUnknownError(err);
234+
235+
// Try to load reporters from a cwd-relative path
236+
try {
237+
map[pluginId] = require(path.resolve(pluginId));
238+
} catch (e) {
239+
throw createUnknownError(e);
240240
}
241241
}
242242
}

lib/mocha.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ var Suite = require('./suite');
1616
var esmUtils = require('./nodejs/esm-utils');
1717
var createStatsCollector = require('./stats-collector');
1818
const {
19-
warn,
2019
createInvalidReporterError,
2120
createInvalidInterfaceError,
2221
createMochaInstanceAlreadyDisposedError,
@@ -335,35 +334,26 @@ Mocha.prototype.reporter = function (reporterName, reporterOptions) {
335334
}
336335
// Try to load reporters from process.cwd() and node_modules
337336
if (!reporter) {
337+
let foundReporter;
338338
try {
339-
reporter = require(reporterName);
339+
foundReporter = require.resolve(reporterName);
340+
reporter = require(foundReporter);
340341
} catch (err) {
341-
if (err.code === 'MODULE_NOT_FOUND') {
342-
// Try to load reporters from a path (absolute or relative)
343-
try {
344-
reporter = require(path.resolve(utils.cwd(), reporterName));
345-
} catch (_err) {
346-
_err.code === 'MODULE_NOT_FOUND'
347-
? warn(`'${reporterName}' reporter not found`)
348-
: warn(
349-
`'${reporterName}' reporter blew up with error:\n ${err.stack}`
350-
);
351-
}
352-
} else {
353-
warn(`'${reporterName}' reporter blew up with error:\n ${err.stack}`);
342+
if (foundReporter) {
343+
throw createInvalidReporterError(err.message, foundReporter);
344+
}
345+
// Try to load reporters from a cwd-relative path
346+
try {
347+
reporter = require(path.resolve(reporterName));
348+
} catch (e) {
349+
throw createInvalidReporterError(e.message, reporterName);
354350
}
355351
}
356352
}
357-
if (!reporter) {
358-
throw createInvalidReporterError(
359-
`invalid reporter '${reporterName}'`,
360-
reporterName
361-
);
362-
}
363353
this._reporter = reporter;
364354
}
365355
this.options.reporterOption = reporterOptions;
366-
// alias option name is used in public reporters xunit/tap/progress
356+
// alias option name is used in built-in reporters xunit/tap/progress
367357
this.options.reporterOptions = reporterOptions;
368358
return this;
369359
};

test/browser-specific/fixtures/webpack/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
plugins: [
1818
new FailOnErrorsPlugin({
1919
failOnErrors: true,
20-
failOnWarnings: true
20+
failOnWarnings: false
2121
})
2222
]
2323
};

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ describe('helpers', function () {
7777
{message: /wonky/, code: 'ERR_MOCHA_INVALID_REPORTER'}
7878
);
7979
});
80+
81+
it('should fail and report the original "MODULE_NOT_FOUND" error.message', function () {
82+
expect(
83+
() =>
84+
validateLegacyPlugin(
85+
{
86+
reporter: require.resolve('./fixtures/bad-require.fixture.js')
87+
},
88+
'reporter'
89+
),
90+
'to throw',
91+
{
92+
message: /Error: Cannot find module 'fake'/,
93+
code: 'ERR_MOCHA_INVALID_REPORTER'
94+
}
95+
);
96+
});
8097
});
8198
});
8299

test/node-unit/mocha.spec.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ describe('Mocha', function () {
246246

247247
it('should load from current working directory', function () {
248248
expect(function () {
249-
mocha.reporter('./spec.js');
249+
mocha.reporter('./lib/reporters/spec.js');
250250
}, 'not to throw');
251251
});
252252

@@ -255,7 +255,7 @@ describe('Mocha', function () {
255255
expect(
256256
function () {
257257
mocha.reporter(
258-
'../../test/node-unit/fixtures/wonky-reporter.fixture.js'
258+
'./test/node-unit/fixtures/wonky-reporter.fixture.js'
259259
);
260260
},
261261
'to throw',
@@ -264,19 +264,6 @@ describe('Mocha', function () {
264264
}
265265
);
266266
});
267-
268-
it('should warn about the error before throwing', function () {
269-
try {
270-
mocha.reporter(
271-
'../../test/node-unit/fixtures/wonky-reporter.fixture.js'
272-
);
273-
} catch (ignored) {
274-
} finally {
275-
expect(stubs.errors.warn, 'to have a call satisfying', [
276-
expect.it('to match', /reporter blew up/)
277-
]);
278-
}
279-
});
280267
});
281268
});
282269

@@ -292,7 +279,7 @@ describe('Mocha', function () {
292279
expect(
293280
function () {
294281
mocha.reporter(
295-
'./test/node-unit/fixtures/wonky-reporter.fixture.js'
282+
'../test/node-unit/fixtures/wonky-reporter.fixture.js'
296283
);
297284
},
298285
'to throw',
@@ -301,19 +288,6 @@ describe('Mocha', function () {
301288
}
302289
);
303290
});
304-
305-
it('should warn about the error before throwing', function () {
306-
try {
307-
mocha.reporter(
308-
'./test/node-unit/fixtures/wonky-reporter.fixture.js'
309-
);
310-
} catch (ignored) {
311-
} finally {
312-
expect(stubs.errors.warn, 'to have a call satisfying', [
313-
expect.it('to match', /reporter blew up/)
314-
]);
315-
}
316-
});
317291
});
318292
});
319293
});

0 commit comments

Comments
 (0)