Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Limit the size of 'actual'/'expected' before generating diff (#4638)

Juerg Ba93d759c4c4c
diff --git a/lib/reporters/base.js b/lib/reporters/base.js
index 8395c7df..20d1ccec 100644
--- a/lib/reporters/base.js
+++ b/lib/reporters/base.js
@@ -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);
diff --git a/test/reporters/base.spec.js b/test/reporters/base.spec.js
index 69b31d30..cc809646 100644
--- a/test/reporters/base.spec.js
+++ b/test/reporters/base.spec.js
@@ -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() {