/
diff.test.ts
61 lines (55 loc) · 1.54 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
import { expect, test, vi } from 'vitest'
import { getDefaultColors, setupColors } from '@vitest/utils'
import { displayDiff } from 'vitest/src/node/error'
import { unifiedDiff } from '@vitest/utils/diff'
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(unifiedDiff(objectA, objectB), console as any)
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
"
- Expected - 1
+ Received + 1
{
a: 1,
- b: 3,
+ b: 2,
}
"
`)
})
test('display one line string diff', () => {
const string1 = 'string1'
const string2 = 'string2'
const console = { log: vi.fn(), error: vi.fn() }
setupColors(getDefaultColors())
displayDiff(unifiedDiff(string1, string2), console as any)
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
"
- Expected - 1
+ Received + 1
- 'string2'
+ 'string1'
"
`)
})
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(unifiedDiff(string1, string2), console as any)
expect(console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
"
- Expected - 2
+ Received + 2
+ string1
\`string2
- string2
- string1\`
+ string3\`
"
`)
})