Skip to content
Open
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
16 changes: 16 additions & 0 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const kReadableOperator = {

const kMaxShortStringLength = 12;
const kMaxLongStringLength = 512;
const kMaxDiffLineCount = 1000;
const kMaxDiffLinesToShow = 50;

const kMethodsWithCustomMessageDiff = new SafeSet()
.add('deepStrictEqual')
Expand Down Expand Up @@ -182,6 +184,13 @@ function isSimpleDiff(actual, inspectedActual, expected, inspectedExpected) {
return typeof actual !== 'object' || actual === null || typeof expected !== 'object' || expected === null;
}

function getTruncatedDiffValue(lines) {
if (lines.length > kMaxDiffLinesToShow) {
return `${ArrayPrototypeJoin(ArrayPrototypeSlice(lines, 0, kMaxDiffLinesToShow), '\n')}\n...`;
}
return ArrayPrototypeJoin(lines, '\n');
}

function createErrDiff(actual, expected, operator, customMessage, diffType = 'simple') {
operator = checkOperator(actual, expected, operator);

Expand Down Expand Up @@ -213,6 +222,13 @@ function createErrDiff(actual, expected, operator, customMessage, diffType = 'si
message = ArrayPrototypeJoin(inspectedSplitActual, '\n');
}
header = '';
} else if (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can completely hide the real diff when users actually need it.

diffType !== 'full' &&
inspectedSplitActual.length + inspectedSplitExpected.length > kMaxDiffLineCount
) {
message = `\n${colors.green}+${colors.white} ${getTruncatedDiffValue(inspectedSplitActual)}\n` +
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hardcoded colors bypasses the partialDeepStrictEqual coloring convention

`${colors.red}-${colors.white} ${getTruncatedDiffValue(inspectedSplitExpected)}`;
skipped = true;
} else {
const checkCommaDisparity = actual != null && typeof actual === 'object';
const diff = myersDiff(inspectedSplitActual, inspectedSplitExpected, checkCommaDisparity);
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,19 @@ test('Strict equal with identical objects that are not identical ' +
);
});

test('Strict equal skips line diff for very large objects', () => {
const buffer = Buffer.alloc(1_000);

assert.throws(
() => assert.strictEqual(buffer, [buffer]),
{
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: /Skipped lines[\s\S]*Buffer\(1000\)/
}
);
});

test('Basic valueOf check', () => {
const a = new String(1);
a.valueOf = undefined;
Expand Down
Loading