diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-09-24 11:15:52 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-09-27 14:36:23 +0300 |
commit | 02c44a48943302d07a3b9575402a8992e57dada8 (patch) | |
tree | c31af186edab5c5eb4567efbdd88a05d56dc1dd5 /lib/internal/assert.js | |
parent | b8a8eedf3280f3825812d3f8f0554a186396c9dc (diff) |
assert: reduce diff noise
Assertion errors that produce a diff show a diff for identical entries
in case one side of the comparison has more object properties than the
other one. Those lines are now taken into account and will not show up
as diverging lines anymore.
Refs: https://github.com/nodejs/node/issues/22763
PR-URL: https://github.com/nodejs/node/pull/23048
Refs: https://github.com/nodejs/node/issues/22763
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/assert.js')
-rw-r--r-- | lib/internal/assert.js | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/lib/internal/assert.js b/lib/internal/assert.js index 92c2a1d0af8..d3155cf692e 100644 --- a/lib/internal/assert.js +++ b/lib/internal/assert.js @@ -205,29 +205,42 @@ function createErrDiff(actual, expected, operator) { res += `\n${green}+${white} ${actualLines[i]}`; printedLines++; // Lines diverge - } else if (actualLines[i] !== expectedLines[i]) { - if (cur > 1 && i > 2) { - if (cur > 4) { - res += `\n${blue}...${white}`; - skipped = true; - } else if (cur > 3) { - res += `\n ${actualLines[i - 2]}`; + } else { + const expectedLine = expectedLines[i]; + let actualLine = actualLines[i]; + let divergingLines = actualLine !== expectedLine && + (!actualLine.endsWith(',') || + actualLine.slice(0, -1) !== expectedLine); + if (divergingLines && + expectedLine.endsWith(',') && + expectedLine.slice(0, -1) === actualLine) { + divergingLines = false; + actualLine += ','; + } + if (divergingLines) { + if (cur > 1 && i > 2) { + if (cur > 4) { + res += `\n${blue}...${white}`; + skipped = true; + } else if (cur > 3) { + res += `\n ${actualLines[i - 2]}`; + printedLines++; + } + res += `\n ${actualLines[i - 1]}`; + printedLines++; + } + lastPos = i; + res += `\n${green}+${white} ${actualLine}`; + other += `\n${red}-${white} ${expectedLine}`; + printedLines += 2; + // Lines are identical + } else { + res += other; + other = ''; + if (cur === 1 || i === 0) { + res += `\n ${actualLine}`; printedLines++; } - res += `\n ${actualLines[i - 1]}`; - printedLines++; - } - lastPos = i; - res += `\n${green}+${white} ${actualLines[i]}`; - other += `\n${red}-${white} ${expectedLines[i]}`; - printedLines += 2; - // Lines are identical - } else { - res += other; - other = ''; - if (cur === 1 || i === 0) { - res += `\n ${actualLines[i]}`; - printedLines++; } } // Inspected object to big (Show ~20 rows max) |