Skip to content

Commit

Permalink
Fix an error when fn===undefined (#3580)
Browse files Browse the repository at this point in the history
* Fix an error when fn===undefined

This change fixes an error that occurs when reporter is 'xunit' and run is called without callback: " /rbd/pnpm-volume/a49b9a7c-c3f0-42ba-ae66-612ead86e96c/node_modules/.registry.npmjs.org/mocha/5.2.0/node_modules/mocha/lib/reporters/xunit.js:126
 fn(failures);  ^
TypeError: fn is not a function "

* Add test `.reporter("xunit").run(fn)`

This test has `try catch` block to catch an error if it occurs and write it to the console.

* formatting

* Update mocha.spec.js
  • Loading branch information
Lana-Light authored and boneskull committed Dec 28, 2018
1 parent e825be7 commit ee6d576
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,11 @@ Mocha.prototype.run = function(fn) {
exports.reporters.Base.hideDiff = options.hideDiff;

function done(failures) {
fn = fn || function fn() {};
if (reporter.done) {
reporter.done(failures, fn);
} else {
fn && fn(failures);
fn(failures);
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ describe('Mocha', function() {
});
});

describe('.reporter("xunit").run(fn)', function() {
it('should not raise errors if callback was not provided', function() {
var mocha = new Mocha();
expect(function() {
try {
mocha.reporter('xunit').run();
} catch (e) {
console.log(e);
expect.fail(e.message);
}
}, 'not to throw');
});
});

describe('.addFile()', function() {
it('should add the given file to the files array', function() {
var mocha = new Mocha(opts);
Expand Down

0 comments on commit ee6d576

Please sign in to comment.