From 74f3b17ada009c37de01076bf7637cb4af342231 Mon Sep 17 00:00:00 2001 From: Svetlana <39729453+Lana-Light@users.noreply.github.com> Date: Sat, 29 Dec 2018 01:12:58 +0200 Subject: [PATCH] Fix an error when fn===undefined (#3580) * 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 --- lib/mocha.js | 3 ++- test/unit/mocha.spec.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/mocha.js b/lib/mocha.js index d2ded3bff4..f92cc173ac 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -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); } } diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 82ba0e1468..e17fe2cffb 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -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);