forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chai.ts
126 lines (117 loc) · 3.71 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
import type { ChaiPlugin } from '../../types/chai'
import { SnapshotClient } from './client'
import { stripSnapshotIndentation } from './port/inlineSnapshot'
let _client: SnapshotClient
export function getSnapshotClient(): SnapshotClient {
if (!_client)
_client = new SnapshotClient()
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 threw')
}
export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
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,
test,
message,
isInline: false,
properties,
errorMessage,
})
},
)
}
utils.addMethod(
chai.Assertion.prototype,
'toMatchInlineSnapshot',
function __VITEST_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,
test,
message,
isInline: true,
properties,
inlineSnapshot,
error,
errorMessage,
})
},
)
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),
test,
message,
errorMessage,
})
},
)
utils.addMethod(
chai.Assertion.prototype,
'toThrowErrorMatchingInlineSnapshot',
function __VITEST_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),
test,
message,
inlineSnapshot,
isInline: true,
error,
errorMessage,
})
},
)
}