diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index 5dbf1e7a3413..06eb4dead697 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -241,7 +241,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-class.js b/test/parallel/test-assert-class.js index cccc7a2f36ff..d9a07c86fc19 100644 --- a/test/parallel/test-assert-class.js +++ b/test/parallel/test-assert-class.js @@ -166,8 +166,8 @@ const truncatedBs = 'B\\n'.repeat(10) + '...'; const longStringOfAs = 'A'.repeat(10_000); const longStringOfBs = 'B'.repeat(10_000); -const longLinesOfAsWithEllipsis = longStringOfAs.substring(0, 9_488) + '...'; -const longLinesOFBsWithEllipsis = longStringOfBs.substring(0, 9_488) + '...'; +const longLinesOfAsWithEllipsis = longStringOfAs.substring(0, 512) + '...'; +const longLinesOFBsWithEllipsis = longStringOfBs.substring(0, 512) + '...'; test('Assert class non strict with full diff', () => { const assertInstance = new Assert({ diff: 'full', strict: false }); diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index d443bd54464f..fa5df83fe07d 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -538,7 +538,23 @@ test('Long values should be truncated for display', () => { `${strictEqualMessageStart}+ actual - expected\n\n` + `+ '${'A'.repeat(1000)}'\n- ''\n`); assert.strictEqual(err.actual.length, 1000); - assert.ok(inspect(err).includes(`actual: '${'A'.repeat(488)}...'`)); + assert.ok(inspect(err).includes(`actual: '${'A'.repeat(512)}...'`)); + return true; + }); +}); + +test('Truncated long values should keep the beginning of the string', () => { + // Use a heterogeneous string so the retained portion is distinguishable + // from the discarded portion (a homogeneous string cannot tell the head + // from the tail). The first `kMaxLongStringLength` (512) characters should + // be kept, followed by an ellipsis. + const actual = 'a'.repeat(300) + 'b'.repeat(300); + assert.throws(() => { + assert.strictEqual(actual, ''); + }, (err) => { + const kept = `${'a'.repeat(300)}${'b'.repeat(212)}`; + assert.ok(inspect(err).includes(`actual: '${kept}...'`), + `expected the beginning of the string to be kept, got ${inspect(err)}`); return true; }); });