Skip to content

Commit

Permalink
fix: wrong error thrown while loading reporter (#4842)
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Mar 11, 2022
1 parent 22f9306 commit 241964b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 62 deletions.
20 changes: 10 additions & 10 deletions lib/cli/run-helpers.js
Expand Up @@ -225,18 +225,18 @@ exports.validateLegacyPlugin = (opts, pluginType, map = {}) => {

// if this exists, then it's already loaded, so nothing more to do.
if (!map[pluginId]) {
let foundId;
try {
map[pluginId] = require(pluginId);
foundId = require.resolve(pluginId);
map[pluginId] = require(foundId);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
// Try to load reporters from a path (absolute or relative)
try {
map[pluginId] = require(path.resolve(pluginId));
} catch (err) {
throw createUnknownError(err);
}
} else {
throw createUnknownError(err);
if (foundId) throw createUnknownError(err);

// Try to load reporters from a cwd-relative path
try {
map[pluginId] = require(path.resolve(pluginId));
} catch (e) {
throw createUnknownError(e);
}
}
}
Expand Down
34 changes: 12 additions & 22 deletions lib/mocha.js
Expand Up @@ -16,7 +16,6 @@ var Suite = require('./suite');
var esmUtils = require('./nodejs/esm-utils');
var createStatsCollector = require('./stats-collector');
const {
warn,
createInvalidReporterError,
createInvalidInterfaceError,
createMochaInstanceAlreadyDisposedError,
Expand Down Expand Up @@ -335,35 +334,26 @@ Mocha.prototype.reporter = function (reporterName, reporterOptions) {
}
// Try to load reporters from process.cwd() and node_modules
if (!reporter) {
let foundReporter;
try {
reporter = require(reporterName);
foundReporter = require.resolve(reporterName);
reporter = require(foundReporter);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
// Try to load reporters from a path (absolute or relative)
try {
reporter = require(path.resolve(utils.cwd(), reporterName));
} catch (_err) {
_err.code === 'MODULE_NOT_FOUND'
? warn(`'${reporterName}' reporter not found`)
: warn(
`'${reporterName}' reporter blew up with error:\n ${err.stack}`
);
}
} else {
warn(`'${reporterName}' reporter blew up with error:\n ${err.stack}`);
if (foundReporter) {
throw createInvalidReporterError(err.message, foundReporter);
}
// Try to load reporters from a cwd-relative path
try {
reporter = require(path.resolve(reporterName));
} catch (e) {
throw createInvalidReporterError(e.message, reporterName);
}
}
}
if (!reporter) {
throw createInvalidReporterError(
`invalid reporter '${reporterName}'`,
reporterName
);
}
this._reporter = reporter;
}
this.options.reporterOption = reporterOptions;
// alias option name is used in public reporters xunit/tap/progress
// alias option name is used in built-in reporters xunit/tap/progress
this.options.reporterOptions = reporterOptions;
return this;
};
Expand Down
2 changes: 1 addition & 1 deletion test/browser-specific/fixtures/webpack/webpack.config.js
Expand Up @@ -17,7 +17,7 @@ module.exports = {
plugins: [
new FailOnErrorsPlugin({
failOnErrors: true,
failOnWarnings: true
failOnWarnings: false
})
]
};
17 changes: 17 additions & 0 deletions test/node-unit/cli/run-helpers.spec.js
Expand Up @@ -77,6 +77,23 @@ describe('helpers', function () {
{message: /wonky/, code: 'ERR_MOCHA_INVALID_REPORTER'}
);
});

it('should fail and report the original "MODULE_NOT_FOUND" error.message', function () {
expect(
() =>
validateLegacyPlugin(
{
reporter: require.resolve('./fixtures/bad-require.fixture.js')
},
'reporter'
),
'to throw',
{
message: /Error: Cannot find module 'fake'/,
code: 'ERR_MOCHA_INVALID_REPORTER'
}
);
});
});
});

Expand Down
32 changes: 3 additions & 29 deletions test/node-unit/mocha.spec.js
Expand Up @@ -246,7 +246,7 @@ describe('Mocha', function () {

it('should load from current working directory', function () {
expect(function () {
mocha.reporter('./spec.js');
mocha.reporter('./lib/reporters/spec.js');
}, 'not to throw');
});

Expand All @@ -255,7 +255,7 @@ describe('Mocha', function () {
expect(
function () {
mocha.reporter(
'../../test/node-unit/fixtures/wonky-reporter.fixture.js'
'./test/node-unit/fixtures/wonky-reporter.fixture.js'
);
},
'to throw',
Expand All @@ -264,19 +264,6 @@ describe('Mocha', function () {
}
);
});

it('should warn about the error before throwing', function () {
try {
mocha.reporter(
'../../test/node-unit/fixtures/wonky-reporter.fixture.js'
);
} catch (ignored) {
} finally {
expect(stubs.errors.warn, 'to have a call satisfying', [
expect.it('to match', /reporter blew up/)
]);
}
});
});
});

Expand All @@ -292,7 +279,7 @@ describe('Mocha', function () {
expect(
function () {
mocha.reporter(
'./test/node-unit/fixtures/wonky-reporter.fixture.js'
'../test/node-unit/fixtures/wonky-reporter.fixture.js'
);
},
'to throw',
Expand All @@ -301,19 +288,6 @@ describe('Mocha', function () {
}
);
});

it('should warn about the error before throwing', function () {
try {
mocha.reporter(
'./test/node-unit/fixtures/wonky-reporter.fixture.js'
);
} catch (ignored) {
} finally {
expect(stubs.errors.warn, 'to have a call satisfying', [
expect.it('to match', /reporter blew up/)
]);
}
});
});
});
});
Expand Down

0 comments on commit 241964b

Please sign in to comment.