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
23 changes: 23 additions & 0 deletions benchmark/assert/strict-equal-buffer-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [100],
size: [1000, 8000],
});

function main({ n, size }) {
const buffer = Buffer.alloc(size);

bench.start();
for (let i = 0; i < n; ++i) {
new assert.AssertionError({
actual: buffer,
expected: [buffer],
operator: 'strictEqual',
stackStartFunction: () => {},
});
}
bench.end(n);
}
29 changes: 20 additions & 9 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const kReadableOperator = {

const kMaxShortStringLength = 12;
const kMaxLongStringLength = 512;
const kMaxDiffInputLines = 1000;

const kMethodsWithCustomMessageDiff = new SafeSet()
.add('deepStrictEqual')
Expand Down Expand Up @@ -215,17 +216,27 @@ function createErrDiff(actual, expected, operator, customMessage, diffType = 'si
header = '';
} else {
const checkCommaDisparity = actual != null && typeof actual === 'object';
const diff = myersDiff(inspectedSplitActual, inspectedSplitExpected, checkCommaDisparity);
if (
diffType !== 'full' &&
(inspectedSplitActual.length > kMaxDiffInputLines ||
inspectedSplitExpected.length > kMaxDiffInputLines)
) {
message = `\n${colors.green}+${colors.white} ${addEllipsis(inspectedActual)}\n` +
`${colors.red}-${colors.white} ${addEllipsis(inspectedExpected)}`;
skipped = true;
} else {
const diff = myersDiff(inspectedSplitActual, inspectedSplitExpected, checkCommaDisparity);

const myersDiffMessage = printMyersDiff(diff, operator);
message = myersDiffMessage.message;
const myersDiffMessage = printMyersDiff(diff, operator);
message = myersDiffMessage.message;

if (operator === 'partialDeepStrictEqual') {
header = `${colors.gray}${colors.hasColors ? '' : '+ '}actual${colors.white} ${colors.red}- expected${colors.white}`;
}
if (operator === 'partialDeepStrictEqual') {
header = `${colors.gray}${colors.hasColors ? '' : '+ '}actual${colors.white} ${colors.red}- expected${colors.white}`;
}

if (myersDiffMessage.skipped) {
skipped = true;
if (myersDiffMessage.skipped) {
skipped = true;
}
}
}

Expand All @@ -241,7 +252,7 @@ function addEllipsis(string) {
lines.length = 10;
return `${ArrayPrototypeJoin(lines, '\n')}\n...`;
} else if (string.length > kMaxLongStringLength) {
return `${StringPrototypeSlice(string, kMaxLongStringLength)}...`;
return `${StringPrototypeSlice(string, 0, kMaxLongStringLength)}...`;
}
return string;
}
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,16 @@ test('Output that extends beyond 10 lines should also be truncated for display',
});
});

test('Large strictEqual object diffs are truncated for display', () => {
const buffer = Buffer.alloc(1001);
const err = assert.throws(() => assert.strictEqual(buffer, [buffer]));

assert.strictEqual(err.code, 'ERR_ASSERTION');
assert.match(err.message, /\.\.\. Skipped lines/);
assert.ok(err.message.includes('<Buffer'));
assert.ok(err.message.split('\n').length < 40);
});

test('Bad args to AssertionError constructor should throw TypeError.', () => {
const args = [1, true, false, '', null, Infinity, Symbol('test'), undefined];
for (const input of args) {
Expand Down
Loading