Skip to content

Commit

Permalink
fix: dont'call fn twice in toThrow (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 11, 2022
1 parent f395b18 commit c472614
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/vitest/src/integrations/chai/jest-expect.ts
Expand Up @@ -372,6 +372,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
)
})
def(['toThrow', 'toThrowError'], function(expected?: string | Constructable | RegExp | Error) {
if (typeof expected === 'string' || typeof expected === 'undefined' || expected instanceof RegExp)
return this.throws(expected)

const obj = this._obj
const promise = utils.flag(this, 'promise')
let thrown: any = null
Expand Down Expand Up @@ -399,7 +402,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
)
}

if (expected && expected instanceof Error) {
if (expected instanceof Error) {
return this.assert(
thrown && expected.message === thrown.message,
`expected error to have message: ${expected.message}`,
Expand All @@ -409,7 +412,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
)
}

if (expected && typeof (expected as any).asymmetricMatch === 'function') {
if (typeof (expected as any).asymmetricMatch === 'function') {
const matcher = expected as any as AsymmetricMatcher<any>
return this.assert(
thrown && matcher.asymmetricMatch(thrown),
Expand All @@ -420,7 +423,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
)
}

return this.to.throws(expected)
throw new Error(`"toThrow" expects string, RegExp, function, Error instance or asymmetric matcher, got "${typeof expected}"`)
})
def(['toHaveReturned', 'toReturn'], function() {
const spy = getSpy(this)
Expand Down

0 comments on commit c472614

Please sign in to comment.