-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
chai.ts
165 lines (152 loc) · 4.73 KB
/
chai.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
156
157
158
159
160
161
162
163
164
165
import type { ChaiPlugin } from '@vitest/expect'
import type { Test } from '@vitest/runner'
import { getNames } from '@vitest/runner/utils'
import type { SnapshotClient } from '@vitest/snapshot'
import { addSerializer, stripSnapshotIndentation } from '@vitest/snapshot'
import { VitestSnapshotClient } from './client'
let _client: SnapshotClient
export function getSnapshotClient(): SnapshotClient {
if (!_client)
_client = new VitestSnapshotClient()
return _client
}
const getErrorMessage = (err: unknown) => {
if (err instanceof Error)
return err.message
return err
}
const getErrorString = (expected: () => void | Error, promise: string | undefined) => {
if (typeof expected !== 'function') {
if (!promise)
throw new Error(`expected must be a function, received ${typeof expected}`)
// when "promised", it receives thrown error
return getErrorMessage(expected)
}
try {
expected()
}
catch (e) {
return getErrorMessage(e)
}
throw new Error('snapshot function didn\'t throw')
}
export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
const getTestNames = (test?: Test) => {
if (!test)
return {}
return {
filepath: test.file?.filepath,
name: getNames(test).slice(1).join(' > '),
}
}
for (const key of ['matchSnapshot', 'toMatchSnapshot']) {
utils.addMethod(
chai.Assertion.prototype,
key,
function (this: Record<string, unknown>, properties?: object, message?: string) {
const expected = utils.flag(this, 'object')
const test = utils.flag(this, 'vitest-test')
if (typeof properties === 'string' && typeof message === 'undefined') {
message = properties
properties = undefined
}
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: expected,
message,
isInline: false,
properties,
errorMessage,
...getTestNames(test),
})
},
)
}
utils.addMethod(
chai.Assertion.prototype,
'toMatchFileSnapshot',
async function (this: Record<string, unknown>, file: string, message?: string) {
const expected = utils.flag(this, 'object')
const test = utils.flag(this, 'vitest-test')
const errorMessage = utils.flag(this, 'message')
await getSnapshotClient().assertRaw({
received: expected,
message,
isInline: false,
rawSnapshot: {
file,
},
errorMessage,
...getTestNames(test),
})
},
)
utils.addMethod(
chai.Assertion.prototype,
'toMatchInlineSnapshot',
function __INLINE_SNAPSHOT__(this: Record<string, unknown>, properties?: object, inlineSnapshot?: string, message?: string) {
const expected = utils.flag(this, 'object')
const error = utils.flag(this, 'error')
const test = utils.flag(this, 'vitest-test')
if (typeof properties === 'string') {
message = inlineSnapshot
inlineSnapshot = properties
properties = undefined
}
if (inlineSnapshot)
inlineSnapshot = stripSnapshotIndentation(inlineSnapshot)
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: expected,
message,
isInline: true,
properties,
inlineSnapshot,
error,
errorMessage,
...getTestNames(test),
})
},
)
utils.addMethod(
chai.Assertion.prototype,
'toThrowErrorMatchingSnapshot',
function (this: Record<string, unknown>, message?: string) {
const expected = utils.flag(this, 'object')
const test = utils.flag(this, 'vitest-test')
const promise = utils.flag(this, 'promise') as string | undefined
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: getErrorString(expected, promise),
message,
errorMessage,
...getTestNames(test),
})
},
)
utils.addMethod(
chai.Assertion.prototype,
'toThrowErrorMatchingInlineSnapshot',
function __INLINE_SNAPSHOT__(this: Record<string, unknown>, inlineSnapshot: string, message: string) {
const expected = utils.flag(this, 'object')
const error = utils.flag(this, 'error')
const test = utils.flag(this, 'vitest-test')
const promise = utils.flag(this, 'promise') as string | undefined
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: getErrorString(expected, promise),
message,
inlineSnapshot,
isInline: true,
error,
errorMessage,
...getTestNames(test),
})
},
)
utils.addMethod(
chai.expect,
'addSnapshotSerializer',
addSerializer,
)
}