@@ -174,6 +174,9 @@ function createErrDiff(actual, expected, operator) {
174
174
// Only extra expected lines exist
175
175
const cur = i - lastPos ;
176
176
if ( actualLines . length < i + 1 ) {
177
+ // If the last diverging line is more than one line above and the
178
+ // current line is at least line three, add some of the former lines and
179
+ // also add dots to indicate skipped entries.
177
180
if ( cur > 1 && i > 2 ) {
178
181
if ( cur > 4 ) {
179
182
res += `\n${ blue } ...${ white } ` ;
@@ -185,11 +188,16 @@ function createErrDiff(actual, expected, operator) {
185
188
res += `\n ${ expectedLines [ i - 1 ] } ` ;
186
189
printedLines ++ ;
187
190
}
191
+ // Mark the current line as the last diverging one.
188
192
lastPos = i ;
193
+ // Add the expected line to the cache.
189
194
other += `\n${ red } -${ white } ${ expectedLines [ i ] } ` ;
190
195
printedLines ++ ;
191
196
// Only extra actual lines exist
192
197
} else if ( expectedLines . length < i + 1 ) {
198
+ // If the last diverging line is more than one line above and the
199
+ // current line is at least line three, add some of the former lines and
200
+ // also add dots to indicate skipped entries.
193
201
if ( cur > 1 && i > 2 ) {
194
202
if ( cur > 4 ) {
195
203
res += `\n${ blue } ...${ white } ` ;
@@ -201,23 +209,40 @@ function createErrDiff(actual, expected, operator) {
201
209
res += `\n ${ actualLines [ i - 1 ] } ` ;
202
210
printedLines ++ ;
203
211
}
212
+ // Mark the current line as the last diverging one.
204
213
lastPos = i ;
214
+ // Add the actual line to the result.
205
215
res += `\n${ green } +${ white } ${ actualLines [ i ] } ` ;
206
216
printedLines ++ ;
207
217
// Lines diverge
208
218
} else {
209
219
const expectedLine = expectedLines [ i ] ;
210
220
let actualLine = actualLines [ i ] ;
221
+ // If the lines diverge, specifically check for lines that only diverge by
222
+ // a trailing comma. In that case it is actually identical and we should
223
+ // mark it as such.
211
224
let divergingLines = actualLine !== expectedLine &&
212
225
( ! actualLine . endsWith ( ',' ) ||
213
226
actualLine . slice ( 0 , - 1 ) !== expectedLine ) ;
227
+ // If the expected line has a trailing comma but is otherwise identical,
228
+ // add a comma at the end of the actual line. Otherwise the output could
229
+ // look weird as in:
230
+ //
231
+ // [
232
+ // 1 // No comma at the end!
233
+ // + 2
234
+ // ]
235
+ //
214
236
if ( divergingLines &&
215
237
expectedLine . endsWith ( ',' ) &&
216
238
expectedLine . slice ( 0 , - 1 ) === actualLine ) {
217
239
divergingLines = false ;
218
240
actualLine += ',' ;
219
241
}
220
242
if ( divergingLines ) {
243
+ // If the last diverging line is more than one line above and the
244
+ // current line is at least line three, add some of the former lines and
245
+ // also add dots to indicate skipped entries.
221
246
if ( cur > 1 && i > 2 ) {
222
247
if ( cur > 4 ) {
223
248
res += `\n${ blue } ...${ white } ` ;
@@ -229,14 +254,21 @@ function createErrDiff(actual, expected, operator) {
229
254
res += `\n ${ actualLines [ i - 1 ] } ` ;
230
255
printedLines ++ ;
231
256
}
257
+ // Mark the current line as the last diverging one.
232
258
lastPos = i ;
259
+ // Add the actual line to the result and cache the expected diverging
260
+ // line so consecutive diverging lines show up as +++--- and not +-+-+-.
233
261
res += `\n${ green } +${ white } ${ actualLine } ` ;
234
262
other += `\n${ red } -${ white } ${ expectedLine } ` ;
235
263
printedLines += 2 ;
236
264
// Lines are identical
237
265
} else {
266
+ // Add all cached information to the result before adding other things
267
+ // and reset the cache.
238
268
res += other ;
239
269
other = '' ;
270
+ // If the last diverging line is exactly one line above or if it is the
271
+ // very first line, add the line to the result.
240
272
if ( cur === 1 || i === 0 ) {
241
273
res += `\n ${ actualLine } ` ;
242
274
printedLines ++ ;
@@ -246,7 +278,7 @@ function createErrDiff(actual, expected, operator) {
246
278
// Inspected object to big (Show ~20 rows max)
247
279
if ( printedLines > 20 && i < maxLines - 2 ) {
248
280
return `${ msg } ${ skippedMsg } \n${ res } \n${ blue } ...${ white } ${ other } \n` +
249
- `${ blue } ...${ white } ` ;
281
+ `${ blue } ...${ white } ` ;
250
282
}
251
283
}
252
284
0 commit comments