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
25 changes: 25 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,19 @@ 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 hasLargeRootMismatch(inspectedActual, inspectedExpected, diffType) {
return diffType !== 'full' &&
inspectedActual.length + inspectedExpected.length > kMaxDiffLineCount &&
inspectedActual[0] !== inspectedExpected[0];
}

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

Expand Down Expand Up @@ -213,6 +228,16 @@ function createErrDiff(actual, expected, operator, customMessage, diffType = 'si
message = ArrayPrototypeJoin(inspectedSplitActual, '\n');
}
header = '';
} else if (hasLargeRootMismatch(inspectedSplitActual, inspectedSplitExpected, diffType)) {
const actualPrefix = operator === 'partialDeepStrictEqual' ?
`${colors.gray}${colors.hasColors ? ' ' : '+'}` :
`${colors.green}+${colors.white}`;
message = `\n${actualPrefix} ${getTruncatedDiffValue(inspectedSplitActual)}\n` +
`${colors.red}-${colors.white} ${getTruncatedDiffValue(inspectedSplitExpected)}`;
skipped = true;
if (operator === 'partialDeepStrictEqual') {
header = `${colors.gray}${colors.hasColors ? '' : '+ '}actual${colors.white} ${colors.red}- expected${colors.white}`;
}
} else {
const checkCommaDisparity = actual != null && typeof actual === 'object';
const diff = myersDiff(inspectedSplitActual, inspectedSplitExpected, checkCommaDisparity);
Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,34 @@ 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('Deep strict equal preserves line diff for large values with the same root', () => {
const actual = new Array(1_000).fill(0);
const expected = new Array(1_000).fill(0);
expected[10] = 1;

assert.throws(
() => assert.deepStrictEqual(actual, expected),
{
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: /- {3}1/
}
);
});

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