Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the size of 'actual'/'expected' strings before generating a diff #4638

Merged
merged 2 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 = '';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I'm trying to contribute to Mocha and before I do I'm trying to get idea of mocha's coding style. I want to know why you used 'var' instea of 'const' or 'let'. Thank you in advance.

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