Skip to content

Commit

Permalink
XUnit reporter should handle exceptions during diff generation (#4068)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgroothuijsen authored and juergba committed Dec 22, 2019
1 parent d9f5079 commit 1412dc8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
20 changes: 15 additions & 5 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ exports.cursor = {
}
};

function showDiff(err) {
var showDiff = (exports.showDiff = function(err) {
return (
err &&
err.showDiff !== false &&
sameType(err.actual, err.expected) &&
err.expected !== undefined
);
}
});

function stringifyDiffObjs(err) {
if (!utils.isString(err.actual) || !utils.isString(err.expected)) {
Expand All @@ -182,9 +182,19 @@ function stringifyDiffObjs(err) {
* @return {string} Diff
*/
var generateDiff = (exports.generateDiff = function(actual, expected) {
return exports.inlineDiffs
? inlineDiff(actual, expected)
: unifiedDiff(actual, expected);
try {
return exports.inlineDiffs
? inlineDiff(actual, expected)
: unifiedDiff(actual, expected);
} catch (err) {
var msg =
'\n ' +
color('diff added', '+ expected') +
' ' +
color('diff removed', '- actual: failed to generate Mocha diff') +
'\n';
return msg;
}
});

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/reporters/xunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ XUnit.prototype.test = function(test) {
if (test.state === STATE_FAILED) {
var err = test.err;
var diff =
Base.hideDiff || !err.actual || !err.expected
? ''
: '\n' + Base.generateDiff(err.actual, err.expected);
!Base.hideDiff && Base.showDiff(err)
? '\n' + Base.generateDiff(err.actual, err.expected)
: '';
this.write(
tag(
'testcase',
Expand Down
36 changes: 36 additions & 0 deletions test/reporters/xunit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,42 @@ describe('XUnit reporter', function() {
'</failure></testcase>';
expect(expectedWrite, 'to be', expectedTag);
});

it('should handle non-string diff values', function() {
var runner = new EventEmitter();
createStatsCollector(runner);
var xunit = new XUnit(runner);

var expectedTest = {
state: STATE_FAILED,
title: expectedTitle,
parent: {
fullTitle: function() {
return expectedClassName;
}
},
duration: 1000,
err: {
actual: 1,
expected: 2,
message: expectedMessage,
stack: expectedStack
}
};

sandbox.stub(xunit, 'write').callsFake(function(str) {
expectedWrite += str;
});

runner.emit(EVENT_TEST_FAIL, expectedTest, expectedTest.err);
runner.emit(EVENT_RUN_END);
sandbox.restore();

var expectedDiff =
'\n + expected - actual\n\n -1\n +2\n ';

expect(expectedWrite, 'to contain', expectedDiff);
});
});

describe('on test pending', function() {
Expand Down

0 comments on commit 1412dc8

Please sign in to comment.