2020-07-28 10:58:37 +08:00
|
|
|
expect.extend({
|
|
|
|
toHaveBeenWarned(received: string) {
|
|
|
|
asserted.add(received)
|
2022-01-21 15:36:48 +08:00
|
|
|
const passed = warn.mock.calls.some(args => args[0].includes(received))
|
2020-07-28 10:58:37 +08:00
|
|
|
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: () =>
|
2020-08-25 09:23:08 +08:00
|
|
|
`expected "${received}" to have been warned` +
|
|
|
|
(msgs.length
|
|
|
|
? `.\n\nActual messages:\n\n - ${msgs}`
|
|
|
|
: ` but no warning was recorded.`)
|
2020-07-28 10:58:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
toHaveBeenWarnedLast(received: string) {
|
|
|
|
asserted.add(received)
|
|
|
|
const passed =
|
2022-01-21 15:36:48 +08:00
|
|
|
warn.mock.calls[warn.mock.calls.length - 1][0].includes(received)
|
2020-07-28 10:58:37 +08:00
|
|
|
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, n: number) {
|
|
|
|
asserted.add(received)
|
|
|
|
let found = 0
|
|
|
|
warn.mock.calls.forEach(args => {
|
2022-01-21 15:36:48 +08:00
|
|
|
if (args[0].includes(received)) {
|
2020-07-28 10:58:37 +08:00
|
|
|
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: jest.SpyInstance
|
|
|
|
const asserted: Set<string> = new Set()
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
asserted.clear()
|
|
|
|
warn = jest.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 => {
|
2022-01-21 15:36:48 +08:00
|
|
|
return received.includes(assertedMsg)
|
2020-07-28 10:58:37 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
warn.mockRestore()
|
|
|
|
if (nonAssertedWarnings.length) {
|
2020-07-28 12:24:12 +08:00
|
|
|
throw new Error(
|
|
|
|
`test case threw unexpected warnings:\n - ${nonAssertedWarnings.join(
|
|
|
|
'\n - '
|
|
|
|
)}`
|
|
|
|
)
|
2020-07-28 10:58:37 +08:00
|
|
|
}
|
|
|
|
})
|