Skip to content

Commit 02c44a4

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 PR-URL: nodejs#23048 Refs: nodejs#22763 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Shingo Inoue <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b8a8eed commit 02c44a4

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

lib/internal/assert.js

+34-21
Original file line numberDiff line numberDiff line change
@@ -205,29 +205,42 @@ function createErrDiff(actual, expected, operator) {
205205
res += `\n${green}+${white} ${actualLines[i]}`;
206206
printedLines++;
207207
// Lines diverge
208-
} else if (actualLines[i] !== expectedLines[i]) {
209-
if (cur > 1 && i > 2) {
210-
if (cur > 4) {
211-
res += `\n${blue}...${white}`;
212-
skipped = true;
213-
} else if (cur > 3) {
214-
res += `\n ${actualLines[i - 2]}`;
208+
} else {
209+
const expectedLine = expectedLines[i];
210+
let actualLine = actualLines[i];
211+
let divergingLines = actualLine !== expectedLine &&
212+
(!actualLine.endsWith(',') ||
213+
actualLine.slice(0, -1) !== expectedLine);
214+
if (divergingLines &&
215+
expectedLine.endsWith(',') &&
216+
expectedLine.slice(0, -1) === actualLine) {
217+
divergingLines = false;
218+
actualLine += ',';
219+
}
220+
if (divergingLines) {
221+
if (cur > 1 && i > 2) {
222+
if (cur > 4) {
223+
res += `\n${blue}...${white}`;
224+
skipped = true;
225+
} else if (cur > 3) {
226+
res += `\n ${actualLines[i - 2]}`;
227+
printedLines++;
228+
}
229+
res += `\n ${actualLines[i - 1]}`;
230+
printedLines++;
231+
}
232+
lastPos = i;
233+
res += `\n${green}+${white} ${actualLine}`;
234+
other += `\n${red}-${white} ${expectedLine}`;
235+
printedLines += 2;
236+
// Lines are identical
237+
} else {
238+
res += other;
239+
other = '';
240+
if (cur === 1 || i === 0) {
241+
res += `\n ${actualLine}`;
215242
printedLines++;
216243
}
217-
res += `\n ${actualLines[i - 1]}`;
218-
printedLines++;
219-
}
220-
lastPos = i;
221-
res += `\n${green}+${white} ${actualLines[i]}`;
222-
other += `\n${red}-${white} ${expectedLines[i]}`;
223-
printedLines += 2;
224-
// Lines are identical
225-
} else {
226-
res += other;
227-
other = '';
228-
if (cur === 1 || i === 0) {
229-
res += `\n ${actualLines[i]}`;
230-
printedLines++;
231244
}
232245
}
233246
// Inspected object to big (Show ~20 rows max)

test/parallel/test-assert-deep.js

+16-8
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)