Skip to content

assert: keep the beginning of long strings when truncating - #65176

Open
arhxam wants to merge 1 commit into
nodejs:mainfrom
arhxam:assert-keep-head-on-truncate
Open

assert: keep the beginning of long strings when truncating#65176
arhxam wants to merge 1 commit into
nodejs:mainfrom
arhxam:assert-keep-head-on-truncate

Conversation

@arhxam

@arhxam arhxam commented Aug 10, 2026

Copy link
Copy Markdown

Description

addEllipsis() in lib/internal/assert/assertion_error.js truncates the
long single-line actual / expected values shown by AssertionError's
custom util.inspect output. For a string longer than
kMaxLongStringLength (512) it did:

return `${StringPrototypeSlice(string, kMaxLongStringLength)}...`;

StringPrototypeSlice(string, 512) drops the first 512 characters and
keeps the tail
, then appends ... as if the end had been cut off —
so the ellipsis is misleading and the wrong part of the string is shown.

Reproduction on current main:

const assert = require('node:assert');
const util = require('node:util');
let err;
try { assert.strictEqual('a'.repeat(300) + 'b'.repeat(300), 'x'); }
catch (e) { err = e; }
console.log(util.inspect(err));
// actual: 'bbbbbbbb…b...'   <- only the last 88 chars survive; no 'a' at all,
//                              and the trailing '...' implies the end was cut

The three sibling truncations in the same file and the multi-line branch
of this very function all keep the beginning of the string. This
changes the single-line branch to StringPrototypeSlice(string, 0, kMaxLongStringLength) so the first 512 characters are kept, consistent
with the trailing ....

Why the existing tests did not catch it

The existing tests truncated homogeneous strings ('A'.repeat(n)), where
the head and tail are identical, so the buggy tail length
(n - 512) was baked into the expectations (e.g. 'A'.repeat(488),
substring(0, 9_488)). Those expectations are corrected to 512, and a
new heterogeneous regression test ('a'.repeat(300) + 'b'.repeat(300)) is
added that actually distinguishes the beginning from the end.

Verification

  • Reproduced the bug on a from-source build of main, then confirmed the
    fix flips it (the new test fails on unpatched source, passes with the fix).
  • python3 tools/test.py on all test/parallel/test-assert*.js (14 files)
    and test-util-inspect.js: all pass.

`addEllipsis()` truncates the long single-line `actual`/`expected`
values shown in `AssertionError`'s custom inspect output. For a string
longer than `kMaxLongStringLength` (512) it called
`StringPrototypeSlice(string, kMaxLongStringLength)`, which drops the
first 512 characters and keeps the tail, then appends `...` as if the
end had been cut off. The three sibling truncations in the same file
and the multi-line branch of this same function all keep the beginning
of the string instead.

Pass a start index of `0` so the first 512 characters are kept, which
matches the sibling behaviour and the trailing `...`.

The existing tests used homogeneous strings (`'A'.repeat(n)`), so the
head and tail were indistinguishable and the buggy tail length was
baked into the expectations; update those and add a heterogeneous
regression test that actually distinguishes the beginning from the end.

Signed-off-by: Arham Wani <arhamwani765@gmail.com>
@nodejs-github-bot nodejs-github-bot added assert Issues and PRs related to the assert subsystem. needs-ci PRs that need a full CI run. labels Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

assert Issues and PRs related to the assert subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants