Skip to content

Commit a93d759

Browse files
authored
Limit the size of 'actual'/'expected' before generating diff (#4638)
1 parent 9b4435d commit a93d759

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/reporters/base.js

+7
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ function stringifyDiffObjs(err) {
190190
*/
191191
var generateDiff = (exports.generateDiff = function(actual, expected) {
192192
try {
193+
const diffSize = 2048;
194+
if (actual.length > diffSize) {
195+
actual = actual.substring(0, diffSize) + ' ... Lines skipped';
196+
}
197+
if (expected.length > diffSize) {
198+
expected = expected.substring(0, diffSize) + ' ... Lines skipped';
199+
}
193200
return exports.inlineDiffs
194201
? inlineDiff(actual, expected)
195202
: unifiedDiff(actual, expected);

test/reporters/base.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,34 @@ describe('Base reporter', function() {
164164
' \n actual expected\n \n a foobar inline diff\n '
165165
);
166166
});
167+
168+
it("should truncate overly long 'actual' ", function() {
169+
var actual = '';
170+
var i = 0;
171+
while (i++ < 120) {
172+
actual += 'a foo unified diff ';
173+
}
174+
var expected = 'a bar unified diff';
175+
176+
inlineDiffsStub.value(false);
177+
var output = generateDiff(actual, expected);
178+
179+
expect(output, 'to match', / \.\.\. Lines skipped/);
180+
});
181+
182+
it("should truncate overly long 'expected' ", function() {
183+
var actual = 'a foo unified diff';
184+
var expected = '';
185+
var i = 0;
186+
while (i++ < 120) {
187+
expected += 'a bar unified diff ';
188+
}
189+
190+
inlineDiffsStub.value(false);
191+
var output = generateDiff(actual, expected);
192+
193+
expect(output, 'to match', / \.\.\. Lines skipped/);
194+
});
167195
});
168196

169197
describe('inline strings diff', function() {

0 commit comments

Comments
 (0)