2023-12-22 21:55:43 +08:00
|
|
|
import type { MockInstance } from 'vitest'
|
2023-01-26 15:25:55 +08:00
|
|
|
|
2023-12-08 18:25:01 +08:00
|
|
|
vi.stubGlobal('MathMLElement', class MathMLElement {})
|
|
|
|
|
2023-01-26 15:25:55 +08:00
|
|
|
expect.extend({
|
|
|
|
toHaveBeenWarned(received: string) {
|
|
|
|
const passed = warn.mock.calls.some(args => args[0].includes(received))
|
|
|
|
if (passed) {
|
2024-02-09 09:02:52 +08:00
|
|
|
asserted.add(received)
|
2023-01-26 15:25:55 +08:00
|
|
|
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` +
|
|
|
|
(msgs.length
|
|
|
|
? `.\n\nActual messages:\n\n - ${msgs}`
|
|
|
|
: ` but no warning was recorded.`),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
toHaveBeenWarnedLast(received: string) {
|
|
|
|
const passed =
|
|
|
|
warn.mock.calls[warn.mock.calls.length - 1][0].includes(received)
|
|
|
|
if (passed) {
|
2024-02-09 09:02:52 +08:00
|
|
|
asserted.add(received)
|
2023-01-26 15:25:55 +08:00
|
|
|
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, n: number) {
|
|
|
|
let found = 0
|
|
|
|
warn.mock.calls.forEach(args => {
|
|
|
|
if (args[0].includes(received)) {
|
|
|
|
found++
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (found === n) {
|
2024-02-09 09:02:52 +08:00
|
|
|
asserted.add(received)
|
2023-01-26 15:25:55 +08:00
|
|
|
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}.`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-12-22 21:55:43 +08:00
|
|
|
let warn: MockInstance
|
2023-01-26 15:25:55 +08:00
|
|
|
const asserted: Set<string> = new Set()
|
|
|
|
|
|
|
|
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(assertedMsg => {
|
|
|
|
return received.includes(assertedMsg)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
warn.mockRestore()
|
|
|
|
if (nonAssertedWarnings.length) {
|
|
|
|
throw new Error(
|
|
|
|
`test case threw unexpected warnings:\n - ${nonAssertedWarnings.join(
|
|
|
|
'\n - ',
|
|
|
|
)}`,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|