Skip to content

assert: truncate long strings in diffs to improve DX - #65221

Open
moshams272 wants to merge 2 commits into
nodejs:mainfrom
moshams272:test/truncate-assertion-diffs
Open

assert: truncate long strings in diffs to improve DX#65221
moshams272 wants to merge 2 commits into
nodejs:mainfrom
moshams272:test/truncate-assertion-diffs

Conversation

@moshams272

@moshams272 moshams272 commented Aug 11, 2026

Copy link
Copy Markdown

Summary

This PR improves the developer experience (DX) of assertion error diffs.

Changes:

  • In non-colored diffs (getStackedDiff): Truncate extremely long strings to a 15-character context around the actual difference. It replaces the rest of the identical string with ... (totaling 18 characters). This keeps the output clean and readable while preserving the position of the ^ indicator.
    Comparison between before & after:
    image

  • In colored diffs (printSimpleMyersDiff): We use the same truncation strategy here as in the non-colored diffs with 10-character context around the actual difference and ...(totaling 13 characters). However, while the non-colored diff only targets the first difference, the Myers diff evaluates all differences in the strings. Because the original algorithm processes character-by-character, we needed to know exactly how many identical characters exist between each difference in order to replace long identical sequences with .... So, we grouped the identical characters first to enable this truncation. (As a bonus, this grouping reduces the ANSI escape code overhead sent to the terminal).
    Comparison between before & after:
    image

  • Tests: Update test-assert.js / test-assert-class.js expectations and regenerate the assertion-color-tty snapshot to reflect these improvements.

@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 11, 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