1
1
import { expect , test , vi } from 'vitest'
2
2
import { getDefaultColors , setupColors } from '@vitest/utils'
3
- import { diff } from '@vitest/utils/diff'
3
+ import type { DiffOptions } from '@vitest/utils/diff'
4
+ import { diff , diffStringsUnified } from '@vitest/utils/diff'
4
5
import { processError } from '@vitest/runner'
5
6
import { displayDiff } from '../../../packages/vitest/src/node/error'
6
7
@@ -24,6 +25,28 @@ test('displays object diff', () => {
24
25
` )
25
26
} )
26
27
28
+ test ( 'display truncated object diff' , ( ) => {
29
+ const objectA = { a : 1 , b : 2 , c : 3 , d : 4 , e : 5 }
30
+ const objectB = { a : 1 , b : 3 , c : 4 , d : 5 , e : 6 }
31
+ const console = { log : vi . fn ( ) , error : vi . fn ( ) }
32
+ setupColors ( getDefaultColors ( ) )
33
+ displayDiff ( diff ( objectA , objectB , { truncateThreshold : 4 } ) , console as any )
34
+ expect ( console . error . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
35
+ "
36
+ - Expected
37
+ + Received
38
+
39
+ Object {
40
+ "a": 1,
41
+ - "b": 2,
42
+ - "c": 3,
43
+ + "b": 3,
44
+ + "c": 4,
45
+ ... Diff result is truncated
46
+ "
47
+ ` )
48
+ } )
49
+
27
50
test ( 'display one line string diff' , ( ) => {
28
51
const string1 = 'string1'
29
52
const string2 = 'string2'
@@ -41,7 +64,24 @@ test('display one line string diff', () => {
41
64
` )
42
65
} )
43
66
44
- test ( 'display multiline line string diff' , ( ) => {
67
+ test ( 'display one line string diff should not be affected by truncateThreshold' , ( ) => {
68
+ const string1 = 'string1'
69
+ const string2 = 'string2'
70
+ const console = { log : vi . fn ( ) , error : vi . fn ( ) }
71
+ setupColors ( getDefaultColors ( ) )
72
+ displayDiff ( diff ( string1 , string2 , { truncateThreshold : 3 } ) , console as any )
73
+ expect ( console . error . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
74
+ "
75
+ - Expected
76
+ + Received
77
+
78
+ - string1
79
+ + string2
80
+ "
81
+ ` )
82
+ } )
83
+
84
+ test ( 'display multiline string diff' , ( ) => {
45
85
const string1 = 'string1\nstring2\nstring3'
46
86
const string2 = 'string2\nstring2\nstring1'
47
87
const console = { log : vi . fn ( ) , error : vi . fn ( ) }
@@ -61,6 +101,46 @@ test('display multiline line string diff', () => {
61
101
` )
62
102
} )
63
103
104
+ test ( 'display truncated multiline string diff' , ( ) => {
105
+ const string1 = 'string1\nstring2\nstring3'
106
+ const string2 = 'string2\nstring2\nstring1'
107
+ const console = { log : vi . fn ( ) , error : vi . fn ( ) }
108
+ setupColors ( getDefaultColors ( ) )
109
+ displayDiff ( diff ( string1 , string2 , { truncateThreshold : 2 } ) , console as any )
110
+ expect ( console . error . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
111
+ "
112
+ - Expected
113
+ + Received
114
+
115
+ - string1
116
+ + string2
117
+ string2
118
+ ... Diff result is truncated
119
+ "
120
+ ` )
121
+ } )
122
+
123
+ test ( 'display truncated multiple items array diff' , ( ) => {
124
+ const array1 = Array ( 45000 ) . fill ( 'foo' )
125
+ const array2 = Array ( 45000 ) . fill ( 'bar' )
126
+ const console = { log : vi . fn ( ) , error : vi . fn ( ) }
127
+ setupColors ( getDefaultColors ( ) )
128
+ displayDiff ( diff ( array1 , array2 , { truncateThreshold : 3 } ) , console as any )
129
+ expect ( console . error . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
130
+ "
131
+ - Expected
132
+ + Received
133
+
134
+ Array [
135
+ - "foo",
136
+ - "foo",
137
+ + "bar",
138
+ + "bar",
139
+ ... Diff result is truncated
140
+ "
141
+ ` )
142
+ } )
143
+
64
144
test ( 'asymmetric matcher in object' , ( ) => {
65
145
setupColors ( getDefaultColors ( ) )
66
146
expect ( getErrorDiff ( { x : 0 , y : 'foo' } , { x : 1 , y : expect . anything ( ) } ) ) . toMatchInlineSnapshot ( `
@@ -75,6 +155,26 @@ test('asymmetric matcher in object', () => {
75
155
` )
76
156
} )
77
157
158
+ test ( 'asymmetric matcher in object with truncated diff' , ( ) => {
159
+ setupColors ( getDefaultColors ( ) )
160
+ expect (
161
+ getErrorDiff (
162
+ { w : 'foo' , x : 0 , y : 'bar' , z : 'baz' } ,
163
+ { w : expect . anything ( ) , x : 1 , y : expect . anything ( ) , z : 'bar' } ,
164
+ { truncateThreshold : 3 } ,
165
+ ) ,
166
+ ) . toMatchInlineSnapshot ( `
167
+ "- Expected
168
+ + Received
169
+
170
+ Object {
171
+ "w": Anything,
172
+ - "x": 1,
173
+ + "x": 0,
174
+ ... Diff result is truncated"
175
+ ` )
176
+ } )
177
+
78
178
test ( 'asymmetric matcher in array' , ( ) => {
79
179
setupColors ( getDefaultColors ( ) )
80
180
expect ( getErrorDiff ( [ 0 , 'foo' ] , [ 1 , expect . anything ( ) ] ) ) . toMatchInlineSnapshot ( `
@@ -89,6 +189,25 @@ test('asymmetric matcher in array', () => {
89
189
` )
90
190
} )
91
191
192
+ test ( 'asymmetric matcher in array with truncated diff' , ( ) => {
193
+ setupColors ( getDefaultColors ( ) )
194
+ expect (
195
+ getErrorDiff (
196
+ [ 0 , 'foo' , 2 ] ,
197
+ [ 1 , expect . anything ( ) , 3 ] ,
198
+ { truncateThreshold : 2 } ,
199
+ ) ,
200
+ ) . toMatchInlineSnapshot ( `
201
+ "- Expected
202
+ + Received
203
+
204
+ Array [
205
+ - 1,
206
+ + 0,
207
+ ... Diff result is truncated"
208
+ ` )
209
+ } )
210
+
92
211
test ( 'asymmetric matcher in nested' , ( ) => {
93
212
setupColors ( getDefaultColors ( ) )
94
213
expect (
@@ -115,6 +234,78 @@ test('asymmetric matcher in nested', () => {
115
234
` )
116
235
} )
117
236
237
+ test ( 'asymmetric matcher in nested with truncated diff' , ( ) => {
238
+ setupColors ( getDefaultColors ( ) )
239
+ expect (
240
+ getErrorDiff (
241
+ [ { x : 0 , y : 'foo' , z : 'bar' } , [ 0 , 'bar' , 'baz' ] ] ,
242
+ [ { x : 1 , y : expect . anything ( ) , z : expect . anything ( ) } , [ 1 , expect . anything ( ) , expect . anything ( ) ] ] ,
243
+ { truncateThreshold : 5 } ,
244
+ ) ,
245
+ ) . toMatchInlineSnapshot ( `
246
+ "- Expected
247
+ + Received
248
+
249
+ Array [
250
+ Object {
251
+ - "x": 1,
252
+ + "x": 0,
253
+ "y": Anything,
254
+ "z": Anything,
255
+ ... Diff result is truncated"
256
+ ` )
257
+ } )
258
+
259
+ test ( 'diff for multi-line string compared by characters' , ( ) => {
260
+ const string1 = `
261
+ foo,
262
+ bar,
263
+ `
264
+ const string2 = `
265
+ FOO,
266
+ bar,
267
+ `
268
+ setupColors ( getDefaultColors ( ) )
269
+ expect (
270
+ diffStringsUnified ( string1 , string2 ) ,
271
+ ) . toMatchInlineSnapshot ( `
272
+ "- Expected
273
+ + Received
274
+
275
+
276
+ - foo,
277
+ + FOO,
278
+ bar,
279
+ "
280
+ ` )
281
+ } )
282
+
283
+ test ( 'truncated diff for multi-line string compared by characters' , ( ) => {
284
+ const string1 = `
285
+ foo,
286
+ bar,
287
+ baz,
288
+ `
289
+ const string2 = `
290
+ FOO,
291
+ bar,
292
+ BAZ,
293
+ `
294
+ setupColors ( getDefaultColors ( ) )
295
+ expect (
296
+ diffStringsUnified ( string1 , string2 , { truncateThreshold : 3 } ) ,
297
+ ) . toMatchInlineSnapshot ( `
298
+ "- Expected
299
+ + Received
300
+
301
+
302
+ - foo,
303
+ + FOO,
304
+ bar,
305
+ ... Diff result is truncated"
306
+ ` )
307
+ } )
308
+
118
309
test ( 'getter only property' , ( ) => {
119
310
setupColors ( getDefaultColors ( ) )
120
311
const x = { normalProp : 1 }
@@ -143,12 +334,12 @@ test('getter only property', () => {
143
334
` )
144
335
} )
145
336
146
- function getErrorDiff ( actual : unknown , expected : unknown ) {
337
+ function getErrorDiff ( actual : unknown , expected : unknown , options ?: DiffOptions ) {
147
338
try {
148
339
expect ( actual ) . toEqual ( expected )
149
340
}
150
341
catch ( e ) {
151
- const error = processError ( e )
342
+ const error = processError ( e , options )
152
343
return error . diff
153
344
}
154
345
expect . unreachable ( )
0 commit comments