Skip to content

Commit

Permalink
assert: reduce diff noise
Browse files Browse the repository at this point in the history
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: #22763

PR-URL: #23048
Refs: #22763
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR committed Sep 27, 2018
1 parent b8a8eed commit 02c44a4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
55 changes: 34 additions & 21 deletions lib/internal/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 16 additions & 8 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ assert.deepEqual(arr, buf);
() => assert.deepStrictEqual(buf2, buf),
{
code: 'ERR_ASSERTION',
message: `${defaultMsgStartFull}\n\n` +
' Buffer [Uint8Array] [\n 120,\n 121,\n 122,\n' +
'+ 10,\n+ prop: 1\n- 10\n ]'
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
' Buffer [Uint8Array] [\n' +
' 120,\n' +
'...\n' +
' 10,\n' +
'+ prop: 1\n' +
' ]'
}
);
assert.deepEqual(buf2, buf);
Expand All @@ -80,9 +84,13 @@ assert.deepEqual(arr, buf);
() => assert.deepStrictEqual(arr, arr2),
{
code: 'ERR_ASSERTION',
message: `${defaultMsgStartFull}\n\n` +
' Uint8Array [\n 120,\n 121,\n 122,\n' +
'+ 10\n- 10,\n- prop: 5\n ]'
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
' Uint8Array [\n' +
' 120,\n' +
'...\n' +
' 10,\n' +
'- prop: 5\n' +
' ]'
}
);
assert.deepEqual(arr, arr2);
Expand Down Expand Up @@ -822,7 +830,7 @@ assert.throws(
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
message: `${defaultMsgStartFull}\n\n ` +
'{\n+ a: 4\n- a: 4,\n- b: true\n }'
'{\n a: 4,\n- b: true\n }'
});
assert.throws(
() => assert.deepStrictEqual(['a'], { 0: 'a' }),
Expand Down Expand Up @@ -891,7 +899,7 @@ assert.deepStrictEqual(obj1, obj2);
assert.throws(
() => assert.deepStrictEqual(arrProxy, [1, 2, 3]),
{ message: `${defaultMsgStartFull}\n\n` +
' [\n 1,\n+ 2\n- 2,\n- 3\n ]' }
' [\n 1,\n 2,\n- 3\n ]' }
);
util.inspect.defaultOptions = tmp;

Expand Down

0 comments on commit 02c44a4

Please sign in to comment.