Skip to content

Commit 1e6bd03

Browse files
committed
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: nodejs#22763
1 parent de0441f commit 1e6bd03

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

lib/internal/assert.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -191,29 +191,42 @@ function createErrDiff(actual, expected, operator) {
191191
res += `\n${green}+${white} ${actualLines[i]}`;
192192
printedLines++;
193193
// Lines diverge
194-
} else if (actualLines[i] !== expectedLines[i]) {
195-
if (cur > 1 && i > 2) {
196-
if (cur > 4) {
197-
res += `\n${blue}...${white}`;
198-
skipped = true;
199-
} else if (cur > 3) {
200-
res += `\n ${actualLines[i - 2]}`;
194+
} else {
195+
const expectedLine = expectedLines[i];
196+
let actualLine = actualLines[i];
197+
let divergingLines = actualLine !== expectedLine &&
198+
(!actualLine.endsWith(',') ||
199+
actualLine.slice(0, -1) !== expectedLine);
200+
if (divergingLines &&
201+
expectedLine.endsWith(',') &&
202+
expectedLine.slice(0, -1) === actualLine) {
203+
divergingLines = false;
204+
actualLine += ',';
205+
}
206+
if (divergingLines) {
207+
if (cur > 1 && i > 2) {
208+
if (cur > 4) {
209+
res += `\n${blue}...${white}`;
210+
skipped = true;
211+
} else if (cur > 3) {
212+
res += `\n ${actualLines[i - 2]}`;
213+
printedLines++;
214+
}
215+
res += `\n ${actualLines[i - 1]}`;
216+
printedLines++;
217+
}
218+
lastPos = i;
219+
res += `\n${green}+${white} ${actualLine}`;
220+
other += `\n${red}-${white} ${expectedLine}`;
221+
printedLines += 2;
222+
// Lines are identical
223+
} else {
224+
res += other;
225+
other = '';
226+
if (cur === 1 || i === 0) {
227+
res += `\n ${actualLine}`;
201228
printedLines++;
202229
}
203-
res += `\n ${actualLines[i - 1]}`;
204-
printedLines++;
205-
}
206-
lastPos = i;
207-
res += `\n${green}+${white} ${actualLines[i]}`;
208-
other += `\n${red}-${white} ${expectedLines[i]}`;
209-
printedLines += 2;
210-
// Lines are identical
211-
} else {
212-
res += other;
213-
other = '';
214-
if (cur === 1 || i === 0) {
215-
res += `\n ${actualLines[i]}`;
216-
printedLines++;
217230
}
218231
}
219232
// Inspected object to big (Show ~20 rows max)

test/parallel/test-assert-deep.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ assert.deepEqual(arr, buf);
6565
() => assert.deepStrictEqual(buf2, buf),
6666
{
6767
code: 'ERR_ASSERTION',
68-
message: `${defaultMsgStartFull}\n\n` +
69-
' Buffer [Uint8Array] [\n 120,\n 121,\n 122,\n' +
70-
'+ 10,\n+ prop: 1\n- 10\n ]'
68+
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
69+
' Buffer [Uint8Array] [\n' +
70+
' 120,\n' +
71+
'...\n' +
72+
' 10,\n' +
73+
'+ prop: 1\n' +
74+
' ]'
7175
}
7276
);
7377
assert.deepEqual(buf2, buf);
@@ -80,9 +84,13 @@ assert.deepEqual(arr, buf);
8084
() => assert.deepStrictEqual(arr, arr2),
8185
{
8286
code: 'ERR_ASSERTION',
83-
message: `${defaultMsgStartFull}\n\n` +
84-
' Uint8Array [\n 120,\n 121,\n 122,\n' +
85-
'+ 10\n- 10,\n- prop: 5\n ]'
87+
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
88+
' Uint8Array [\n' +
89+
' 120,\n' +
90+
'...\n' +
91+
' 10,\n' +
92+
'- prop: 5\n' +
93+
' ]'
8694
}
8795
);
8896
assert.deepEqual(arr, arr2);
@@ -822,7 +830,7 @@ assert.throws(
822830
code: 'ERR_ASSERTION',
823831
name: 'AssertionError [ERR_ASSERTION]',
824832
message: `${defaultMsgStartFull}\n\n ` +
825-
'{\n+ a: 4\n- a: 4,\n- b: true\n }'
833+
'{\n a: 4,\n- b: true\n }'
826834
});
827835
assert.throws(
828836
() => assert.deepStrictEqual(['a'], { 0: 'a' }),
@@ -891,7 +899,7 @@ assert.deepStrictEqual(obj1, obj2);
891899
assert.throws(
892900
() => assert.deepStrictEqual(arrProxy, [1, 2, 3]),
893901
{ message: `${defaultMsgStartFull}\n\n` +
894-
' [\n 1,\n+ 2\n- 2,\n- 3\n ]' }
902+
' [\n 1,\n 2,\n- 3\n ]' }
895903
);
896904
util.inspect.defaultOptions = tmp;
897905

0 commit comments

Comments
 (0)