Skip to content

Commit c472614

Browse files
authored
fix: dont'call fn twice in toThrow (#915)
1 parent f395b18 commit c472614

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/vitest/src/integrations/chai/jest-expect.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
372372
)
373373
})
374374
def(['toThrow', 'toThrowError'], function(expected?: string | Constructable | RegExp | Error) {
375+
if (typeof expected === 'string' || typeof expected === 'undefined' || expected instanceof RegExp)
376+
return this.throws(expected)
377+
375378
const obj = this._obj
376379
const promise = utils.flag(this, 'promise')
377380
let thrown: any = null
@@ -399,7 +402,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
399402
)
400403
}
401404

402-
if (expected && expected instanceof Error) {
405+
if (expected instanceof Error) {
403406
return this.assert(
404407
thrown && expected.message === thrown.message,
405408
`expected error to have message: ${expected.message}`,
@@ -409,7 +412,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
409412
)
410413
}
411414

412-
if (expected && typeof (expected as any).asymmetricMatch === 'function') {
415+
if (typeof (expected as any).asymmetricMatch === 'function') {
413416
const matcher = expected as any as AsymmetricMatcher<any>
414417
return this.assert(
415418
thrown && matcher.asymmetricMatch(thrown),
@@ -420,7 +423,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
420423
)
421424
}
422425

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

0 commit comments

Comments
 (0)