add toHaveBeenWarned and toHaveBeenWarnedWith custom matchers
This commit is contained in:
parent
2de0aa9d91
commit
ab66c5afe3
1 changed files with 74 additions and 0 deletions
|
|
@ -1,6 +1,18 @@
|
|||
const prettier = require('prettier')
|
||||
const { diff } = require('jest-diff')
|
||||
const lightningcss = require('lightningcss')
|
||||
const log = require('../src/util/log').default
|
||||
|
||||
let warn
|
||||
|
||||
beforeEach(() => {
|
||||
warn = jest.spyOn(log, 'warn')
|
||||
warn.mockImplementation(() => {})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
warn.mockRestore()
|
||||
})
|
||||
|
||||
function formatPrettier(input) {
|
||||
return prettier.format(input, {
|
||||
|
|
@ -137,4 +149,66 @@ expect.extend({
|
|||
|
||||
return { actual: received, message, pass }
|
||||
},
|
||||
toHaveBeenWarned() {
|
||||
let passed = warn.mock.calls.length > 0
|
||||
if (passed) {
|
||||
return {
|
||||
pass: true,
|
||||
message: () => {
|
||||
return (
|
||||
this.utils.matcherHint('toHaveBeenWarned') +
|
||||
'\n\n' +
|
||||
`Expected number of calls: >= ${this.utils.printExpected(1)}\n` +
|
||||
`Received number of calls: ${this.utils.printReceived(actualWarningKeys.length)}`
|
||||
)
|
||||
},
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
pass: false,
|
||||
message: () => {
|
||||
return (
|
||||
this.utils.matcherHint('toHaveBeenWarned') +
|
||||
'\n\n' +
|
||||
`Expected number of calls: >= ${this.utils.printExpected(1)}\n` +
|
||||
`Received number of calls: ${this.utils.printReceived(warn.mock.calls.length)}`
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
toHaveBeenWarnedWith(_received, expectedWarningKeys) {
|
||||
let actualWarningKeys = warn.mock.calls.map((args) => args[0])
|
||||
|
||||
let passed = expectedWarningKeys.every((key) => actualWarningKeys.includes(key))
|
||||
if (passed) {
|
||||
return {
|
||||
pass: true,
|
||||
message: () => {
|
||||
return (
|
||||
this.utils.matcherHint('toHaveBeenWarnedWith') +
|
||||
'\n\n' +
|
||||
`Expected: not ${this.utils.printExpected(expectedWarningKeys)}\n` +
|
||||
`Received: ${this.utils.printReceived(actualWarningKeys)}`
|
||||
)
|
||||
},
|
||||
}
|
||||
} else {
|
||||
let diffString = diff(expectedWarningKeys, actualWarningKeys)
|
||||
|
||||
return {
|
||||
pass: false,
|
||||
message: () => {
|
||||
return (
|
||||
this.utils.matcherHint('toHaveBeenWarnedWith') +
|
||||
'\n\n' +
|
||||
(diffString && diffString.includes('- Expect')
|
||||
? `Difference:\n\n${diffString}`
|
||||
: `Expected: ${this.utils.printExpected(expectedWarningKeys)}\n` +
|
||||
`Received: ${this.utils.printReceived(actualWarningKeys)}`)
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue