Skip to content

Commit 8157a50

Browse files
committed
assert: print more lines in the error diff
So far consequitive identical lines were collapsed if there were at least three. Now they are only collapsed from five identical lines on. This also simplifies the implementation a tiny bit by abstracting some logic. PR-URL: #28058 Reviewed-By: Rich Trott <[email protected]>
1 parent 8217441 commit 8157a50

File tree

4 files changed

+77
-57
lines changed

4 files changed

+77
-57
lines changed

lib/internal/assert/assertion_error.js

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function createErrDiff(actual, expected, operator) {
122122
let a = actualLines[actualLines.length - 1];
123123
let b = expectedLines[expectedLines.length - 1];
124124
while (a === b) {
125-
if (i++ < 2) {
125+
if (i++ < 3) {
126126
end = `\n ${a}${end}`;
127127
} else {
128128
other = a;
@@ -154,7 +154,9 @@ function createErrDiff(actual, expected, operator) {
154154
return `${kReadableOperator.notIdentical}\n\n${actualLines.join('\n')}\n`;
155155
}
156156

157-
if (i > 3) {
157+
// There were at least five identical lines at the end. Mark a couple of
158+
// skipped.
159+
if (i >= 5) {
158160
end = `\n${blue}...${white}${end}`;
159161
skipped = true;
160162
}
@@ -169,46 +171,46 @@ function createErrDiff(actual, expected, operator) {
169171
`\n${green}+ actual${white} ${red}- expected${white}`;
170172
const skippedMsg = ` ${blue}...${white} Lines skipped`;
171173

174+
let lines = actualLines;
175+
let plusMinus = `${green}+${white}`;
176+
let maxLength = expectedLines.length;
177+
if (actualLines.length < maxLines) {
178+
lines = expectedLines;
179+
plusMinus = `${red}-${white}`;
180+
maxLength = actualLines.length;
181+
}
182+
172183
for (i = 0; i < maxLines; i++) {
173-
if (actualLines.length < i + 1) {
174-
// If more than one former line is identical, print that. Collapse those
175-
// in case more than three lines before were identical.
176-
if (identical > 1) {
184+
if (maxLength < i + 1) {
185+
// If more than two former lines are identical, print them. Collapse them
186+
// in case more than five lines were identical.
187+
if (identical > 2) {
177188
if (identical > 3) {
178-
res += `\n${blue}...${white}`;
179-
skipped = true;
180-
} else if (identical > 2) {
181-
res += `\n ${expectedLines[i - 2]}`;
189+
if (identical > 4) {
190+
if (identical === 5) {
191+
res += `\n ${lines[i - 3]}`;
192+
printedLines++;
193+
} else {
194+
res += `\n${blue}...${white}`;
195+
skipped = true;
196+
}
197+
}
198+
res += `\n ${lines[i - 2]}`;
182199
printedLines++;
183200
}
184-
res += `\n ${expectedLines[i - 1]}`;
201+
res += `\n ${lines[i - 1]}`;
185202
printedLines++;
186203
}
187204
// No identical lines before.
188205
identical = 0;
189206
// Add the expected line to the cache.
190-
other += `\n${red}-${white} ${expectedLines[i]}`;
191-
printedLines++;
192-
// Only extra actual lines exist
193-
} else if (expectedLines.length < i + 1) {
194-
// If more than one former line is identical, print that. Collapse those
195-
// in case more than three lines before were identical.
196-
if (identical > 1) {
197-
if (identical > 3) {
198-
res += `\n${blue}...${white}`;
199-
skipped = true;
200-
} else if (identical > 2) {
201-
res += `\n ${actualLines[i - 2]}`;
202-
printedLines++;
203-
}
204-
res += `\n ${actualLines[i - 1]}`;
205-
printedLines++;
207+
if (lines === actualLines) {
208+
res += `\n${plusMinus} ${lines[i]}`;
209+
} else {
210+
other += `\n${plusMinus} ${lines[i]}`;
206211
}
207-
// No identical lines before.
208-
identical = 0;
209-
// Add the actual line to the result.
210-
res += `\n${green}+${white} ${actualLines[i]}`;
211212
printedLines++;
213+
// Only extra actual lines exist
212214
// Lines diverge
213215
} else {
214216
const expectedLine = expectedLines[i];
@@ -235,13 +237,19 @@ function createErrDiff(actual, expected, operator) {
235237
actualLine += ',';
236238
}
237239
if (divergingLines) {
238-
// If more than one former line is identical, print that. Collapse those
239-
// in case more than three lines before were identical.
240-
if (identical > 1) {
240+
// If more than two former lines are identical, print them. Collapse
241+
// them in case more than five lines were identical.
242+
if (identical > 2) {
241243
if (identical > 3) {
242-
res += `\n${blue}...${white}`;
243-
skipped = true;
244-
} else if (identical > 2) {
244+
if (identical > 4) {
245+
if (identical === 5) {
246+
res += `\n ${actualLines[i - 3]}`;
247+
printedLines++;
248+
} else {
249+
res += `\n${blue}...${white}`;
250+
skipped = true;
251+
}
252+
}
245253
res += `\n ${actualLines[i - 2]}`;
246254
printedLines++;
247255
}
@@ -264,7 +272,7 @@ function createErrDiff(actual, expected, operator) {
264272
identical++;
265273
// The very first identical line since the last diverging line is be
266274
// added to the result.
267-
if (identical === 1) {
275+
if (identical <= 2) {
268276
res += `\n ${actualLine}`;
269277
printedLines++;
270278
}

test/parallel/test-assert-deep.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ assert.throws(
5353
code: 'ERR_ASSERTION',
5454
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
5555
'+ Uint8Array [\n' +
56-
'- Buffer [Uint8Array] [\n 120,\n...\n 10\n ]'
56+
'- Buffer [Uint8Array] [\n 120,\n...\n 122,\n 10\n ]'
5757
}
5858
);
5959
assert.deepEqual(arr, buf);
@@ -66,9 +66,11 @@ assert.deepEqual(arr, buf);
6666
() => assert.deepStrictEqual(buf2, buf),
6767
{
6868
code: 'ERR_ASSERTION',
69-
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
69+
message: `${defaultMsgStartFull}\n\n` +
7070
' Buffer [Uint8Array] [\n' +
71-
'...\n' +
71+
' 120,\n' +
72+
' 121,\n' +
73+
' 122,\n' +
7274
' 10,\n' +
7375
'+ prop: 1\n' +
7476
' ]'
@@ -84,9 +86,11 @@ assert.deepEqual(arr, buf);
8486
() => assert.deepStrictEqual(arr, arr2),
8587
{
8688
code: 'ERR_ASSERTION',
87-
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
89+
message: `${defaultMsgStartFull}\n\n` +
8890
' Uint8Array [\n' +
89-
'...\n' +
91+
' 120,\n' +
92+
' 121,\n' +
93+
' 122,\n' +
9094
' 10,\n' +
9195
'- prop: 5\n' +
9296
' ]'
@@ -932,17 +936,19 @@ assert.deepStrictEqual(obj1, obj2);
932936
),
933937
{
934938
message: 'Expected values to be strictly deep-equal:\n' +
935-
'+ actual - expected ... Lines skipped\n' +
936-
'\n' +
937-
' Comparison {\n' +
938-
'...\n' +
939-
" \"+ foo: 'bar'\\n\" +\n" +
940-
"+ \"- foo: 'baz.'\\n\" +\n" +
941-
"- \"- foo: 'baz'\\n\" +\n" +
942-
" ' }',\n" +
943-
"+ operator: 'deepStrictEqual'\n" +
944-
"- operator: 'throws'\n" +
945-
' }'
939+
'+ actual - expected ... Lines skipped\n' +
940+
'\n' +
941+
' Comparison {\n' +
942+
" message: 'Expected values to be strictly deep-equal:\\n' +\n" +
943+
'...\n' +
944+
" ' [TypeError: foo] {\\n' +\n" +
945+
" \"+ foo: 'bar'\\n\" +\n" +
946+
"+ \"- foo: 'baz.'\\n\" +\n" +
947+
"- \"- foo: 'baz'\\n\" +\n" +
948+
" ' }',\n" +
949+
"+ operator: 'deepStrictEqual'\n" +
950+
"- operator: 'throws'\n" +
951+
' }'
946952
}
947953
);
948954
}

test/parallel/test-assert.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,14 @@ assert.throws(
481481
'',
482482
' [',
483483
' [',
484-
'...',
484+
' [',
485+
' 1,',
485486
' 2,',
486487
'+ 3',
487488
"- '3'",
488489
' ]',
489490
'...',
491+
' 4,',
490492
' 5',
491493
' ]'].join('\n');
492494
assert.throws(
@@ -500,10 +502,12 @@ assert.throws(
500502
' [',
501503
' 1,',
502504
'...',
505+
' 1,',
503506
' 0,',
504507
'- 1,',
505508
' 1,',
506509
'...',
510+
' 1,',
507511
' 1',
508512
' ]'
509513
].join('\n');
@@ -520,10 +524,11 @@ assert.throws(
520524
' [',
521525
' 1,',
522526
'...',
527+
' 1,',
523528
' 0,',
524529
'+ 1,',
525530
' 1,',
526-
'...',
531+
' 1,',
527532
' 1',
528533
' ]'
529534
].join('\n');

test/pseudo-tty/test-assert-colors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ try {
99
// active.
1010
process.env.TERM = 'FOOBAR';
1111
delete process.env.NODE_DISABLE_COLORS;
12-
assert.deepStrictEqual([1, 2, 2, 2], [2, 2, 2, 2]);
12+
assert.deepStrictEqual([1, 2, 2, 2, 2], [2, 2, 2, 2, 2]);
1313
} catch (err) {
1414
const expected = 'Expected values to be strictly deep-equal:\n' +
1515
'\u001b[32m+ actual\u001b[39m \u001b[31m- expected\u001b[39m' +
@@ -19,6 +19,7 @@ try {
1919
'\u001b[31m-\u001b[39m 2,\n' +
2020
' 2,\n' +
2121
'\u001b[34m...\u001b[39m\n' +
22+
' 2,\n' +
2223
' 2\n' +
2324
' ]';
2425
assert.strictEqual(err.message, expected);

0 commit comments

Comments
 (0)