diff --git a/benchmark/assert/strict-equal-buffer-array.js b/benchmark/assert/strict-equal-buffer-array.js new file mode 100644 index 00000000000000..5f7228d0ec1909 --- /dev/null +++ b/benchmark/assert/strict-equal-buffer-array.js @@ -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); +} diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index 5dbf1e7a341380..1dd3e155a26b15 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -42,6 +42,7 @@ const kReadableOperator = { const kMaxShortStringLength = 12; const kMaxLongStringLength = 512; +const kMaxDiffInputLines = 1000; const kMethodsWithCustomMessageDiff = new SafeSet() .add('deepStrictEqual') @@ -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; + } } } @@ -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; } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index d443bd54464f4b..94060436eff2e0 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -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(' { const args = [1, true, false, '', null, Infinity, Symbol('test'), undefined]; for (const input of args) {