forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.test.ts
155 lines (140 loc) · 3.28 KB
/
diff.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { expect, test, vi } from 'vitest'
import { getDefaultColors, setupColors } from '@vitest/utils'
import { diff } from '@vitest/utils/diff'
import { processError } from '@vitest/runner'
import { displayDiff } from '../../../packages/vitest/src/node/error'
test('displays object diff', () => {
const objectA = { a: 1, b: 2 }
const objectB = { a: 1, b: 3 }
const console = { log: vi.fn(), error: vi.fn() }
setupColors(getDefaultColors())
displayDiff(diff(objectA, objectB), console as any)
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
"
- Expected
+ Received
Object {
"a": 1,
- "b": 2,
+ "b": 3,
}
"
`)
})
test('display one line string diff', () => {
const string1 = 'string1'
const string2 = 'string2'
const console = { log: vi.fn(), error: vi.fn() }
setupColors(getDefaultColors())
displayDiff(diff(string1, string2), console as any)
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
"
- Expected
+ Received
- string1
+ string2
"
`)
})
test('display multiline line string diff', () => {
const string1 = 'string1\nstring2\nstring3'
const string2 = 'string2\nstring2\nstring1'
const console = { log: vi.fn(), error: vi.fn() }
setupColors(getDefaultColors())
displayDiff(diff(string1, string2), console as any)
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
"
- Expected
+ Received
- string1
string2
- string3
+ string2
+ string1
"
`)
})
test('asymmetric matcher in object', () => {
setupColors(getDefaultColors())
expect(getErrorDiff({ x: 0, y: 'foo' }, { x: 1, y: expect.anything() })).toMatchInlineSnapshot(`
"- Expected
+ Received
Object {
- "x": 1,
+ "x": 0,
"y": Anything,
}"
`)
})
test('asymmetric matcher in array', () => {
setupColors(getDefaultColors())
expect(getErrorDiff([0, 'foo'], [1, expect.anything()])).toMatchInlineSnapshot(`
"- Expected
+ Received
Array [
- 1,
+ 0,
Anything,
]"
`)
})
test('asymmetric matcher in nested', () => {
setupColors(getDefaultColors())
expect(
getErrorDiff(
[{ x: 0, y: 'foo' }, [0, 'bar']],
[{ x: 1, y: expect.anything() }, [1, expect.anything()]],
),
).toMatchInlineSnapshot(`
"- Expected
+ Received
Array [
Object {
- "x": 1,
+ "x": 0,
"y": Anything,
},
Array [
- 1,
+ 0,
Anything,
],
]"
`)
})
test('getter only property', () => {
setupColors(getDefaultColors())
const x = { normalProp: 1 }
const y = { normalProp: 2 }
Object.defineProperty(x, 'getOnlyProp', {
enumerable: true,
get: () => ({ a: 'b' }),
})
Object.defineProperty(y, 'getOnlyProp', {
enumerable: true,
get: () => ({ a: 'b' }),
})
expect(
getErrorDiff(x, y),
).toMatchInlineSnapshot(`
"- Expected
+ Received
Object {
"getOnlyProp": Object {
"a": "b",
},
- "normalProp": 2,
+ "normalProp": 1,
}"
`)
})
function getErrorDiff(actual: unknown, expected: unknown) {
try {
expect(actual).toEqual(expected)
}
catch (e) {
const error = processError(e)
return error.diff
}
expect.unreachable()
}