Skip to content

Commit

Permalink
Limit the size of 'actual'/'expected' before generating diff (#4638)
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed May 30, 2021
1 parent 9b4435d commit a93d759
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/reporters/base.js
Expand Up @@ -190,6 +190,13 @@ function stringifyDiffObjs(err) {
*/
var generateDiff = (exports.generateDiff = function(actual, expected) {
try {
const diffSize = 2048;
if (actual.length > diffSize) {
actual = actual.substring(0, diffSize) + ' ... Lines skipped';
}
if (expected.length > diffSize) {
expected = expected.substring(0, diffSize) + ' ... Lines skipped';
}
return exports.inlineDiffs
? inlineDiff(actual, expected)
: unifiedDiff(actual, expected);
Expand Down
28 changes: 28 additions & 0 deletions test/reporters/base.spec.js
Expand Up @@ -164,6 +164,34 @@ describe('Base reporter', function() {
' \n actual expected\n \n a foobar inline diff\n '
);
});

it("should truncate overly long 'actual' ", function() {
var actual = '';
var i = 0;
while (i++ < 120) {
actual += 'a foo unified diff ';
}
var expected = 'a bar unified diff';

inlineDiffsStub.value(false);
var output = generateDiff(actual, expected);

expect(output, 'to match', / \.\.\. Lines skipped/);
});

it("should truncate overly long 'expected' ", function() {
var actual = 'a foo unified diff';
var expected = '';
var i = 0;
while (i++ < 120) {
expected += 'a bar unified diff ';
}

inlineDiffsStub.value(false);
var output = generateDiff(actual, expected);

expect(output, 'to match', / \.\.\. Lines skipped/);
});
});

describe('inline strings diff', function() {
Expand Down

0 comments on commit a93d759

Please sign in to comment.