-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathvitest-mock-warn.ts
119 lines (109 loc) · 3.44 KB
/
vitest-mock-warn.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
// https://github.com/posva/jest-mock-warn/blob/master/src/index.js
import { afterEach, beforeEach, expect, MockInstance, vi } from 'vitest'
export function mockWarn() {
expect.extend({
toHaveBeenWarned(received: string | RegExp) {
asserted.set(received.toString(), received)
const passed = warn.mock.calls.some((args) =>
typeof received === 'string'
? args[0].indexOf(received) > -1
: received.test(args[0])
)
if (passed) {
return {
pass: true,
message: () => `expected "${received}" not to have been warned.`,
}
} else {
const msgs = warn.mock.calls.map((args) => args[0]).join('\n - ')
return {
pass: false,
message: () =>
`expected "${received}" to have been warned.\n\nActual messages:\n\n - ${msgs}`,
}
}
},
toHaveBeenWarnedLast(received: string | RegExp) {
asserted.set(received.toString(), received)
const lastCall = warn.mock.calls[warn.mock.calls.length - 1][0]
const passed =
typeof received === 'string'
? lastCall.indexOf(received) > -1
: received.test(lastCall)
if (passed) {
return {
pass: true,
message: () => `expected "${received}" not to have been warned last.`,
}
} else {
const msgs = warn.mock.calls.map((args) => args[0]).join('\n - ')
return {
pass: false,
message: () =>
`expected "${received}" to have been warned last.\n\nActual messages:\n\n - ${msgs}`,
}
}
},
toHaveBeenWarnedTimes(received: string | RegExp, n: number) {
asserted.set(received.toString(), received)
let found = 0
warn.mock.calls.forEach((args) => {
const isFound =
typeof received === 'string'
? args[0].indexOf(received) > -1
: received.test(args[0])
if (isFound) {
found++
}
})
if (found === n) {
return {
pass: true,
message: () =>
`expected "${received}" to have been warned ${n} times.`,
}
} else {
return {
pass: false,
message: () =>
`expected "${received}" to have been warned ${n} times but got ${found}.`,
}
}
},
})
let warn: MockInstance<typeof console.warn>
const asserted = new Map<string, string | RegExp>()
beforeEach(() => {
asserted.clear()
warn = vi.spyOn(console, 'warn')
warn.mockImplementation(() => {})
})
afterEach(() => {
const assertedArray = Array.from(asserted)
const nonAssertedWarnings = warn.mock.calls
.map((args) => args[0])
.filter((received) => {
return !assertedArray.some(([key, assertedMsg]) => {
return typeof assertedMsg === 'string'
? received.indexOf(assertedMsg) > -1
: assertedMsg.test(received)
})
})
warn.mockRestore()
if (nonAssertedWarnings.length) {
nonAssertedWarnings.forEach((warning) => {
console.warn(warning)
})
throw new Error(`test case threw unexpected warnings.`)
}
})
}
interface MockWarnMatcher<R = unknown> {
toHaveBeenWarned(): void
toHaveBeenWarnedLast(): void
toHaveBeenWarnedTimes(n: number): void
}
declare module 'vitest' {
interface Assertion<T = any> extends MockWarnMatcher<T> {}
interface AsymmetricMatchersContaining extends MockWarnMatcher {}
}