1- import { AssertionError } from 'chai'
21import { assertTypes , getColors } from '@vitest/utils'
32import type { Constructable } from '@vitest/utils'
43import type { EnhancedSpy } from '@vitest/spy'
@@ -13,6 +12,7 @@ import { recordAsyncExpect, wrapSoft } from './utils'
1312
1413// Jest Expect Compact
1514export const JestChaiExpect : ChaiPlugin = ( chai , utils ) => {
15+ const { AssertionError } = chai
1616 const c = ( ) => getColors ( )
1717
1818 function def ( name : keyof Assertion | ( keyof Assertion ) [ ] , fn : ( ( this : Chai . AssertionStatic & Assertion , ...args : any [ ] ) => any ) ) {
@@ -436,19 +436,16 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
436436 if ( called && isNot )
437437 msg = formatCalls ( spy , msg )
438438
439- if ( ( called && isNot ) || ( ! called && ! isNot ) ) {
440- const err = new Error ( msg )
441- err . name = 'AssertionError'
442- throw err
443- }
439+ if ( ( called && isNot ) || ( ! called && ! isNot ) )
440+ throw new AssertionError ( msg )
444441 } )
445442 def ( [ 'toHaveBeenCalledWith' , 'toBeCalledWith' ] , function ( ...args ) {
446443 const spy = getSpy ( this )
447444 const spyName = spy . getMockName ( )
448445 const pass = spy . mock . calls . some ( callArg => jestEquals ( callArg , args , [ iterableEquality ] ) )
449446 const isNot = utils . flag ( this , 'negate' ) as boolean
450447
451- let msg = utils . getMessage (
448+ const msg = utils . getMessage (
452449 this ,
453450 [
454451 pass ,
@@ -458,12 +455,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
458455 ] ,
459456 )
460457
461- if ( ( pass && isNot ) || ( ! pass && ! isNot ) ) {
462- msg = formatCalls ( spy , msg , args )
463- const err = new Error ( msg )
464- err . name = 'AssertionError'
465- throw err
466- }
458+ if ( ( pass && isNot ) || ( ! pass && ! isNot ) )
459+ throw new AssertionError ( formatCalls ( spy , msg , args ) )
467460 } )
468461 def ( [ 'toHaveBeenNthCalledWith' , 'nthCalledWith' ] , function ( times : number , ...args : any [ ] ) {
469462 const spy = getSpy ( this )
@@ -595,7 +588,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
595588 const pass = spy . mock . results . some ( ( { type, value : result } ) => type === 'return' && jestEquals ( value , result ) )
596589 const isNot = utils . flag ( this , 'negate' ) as boolean
597590
598- let msg = utils . getMessage (
591+ const msg = utils . getMessage (
599592 this ,
600593 [
601594 pass ,
@@ -605,12 +598,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
605598 ] ,
606599 )
607600
608- if ( ( pass && isNot ) || ( ! pass && ! isNot ) ) {
609- msg = formatReturns ( spy , msg , value )
610- const err = new Error ( msg )
611- err . name = 'AssertionError'
612- throw err
613- }
601+ if ( ( pass && isNot ) || ( ! pass && ! isNot ) )
602+ throw new AssertionError ( formatReturns ( spy , msg , value ) )
614603 } )
615604 def ( [ 'toHaveLastReturnedWith' , 'lastReturnedWith' ] , function ( value : any ) {
616605 const spy = getSpy ( this )
@@ -650,8 +639,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
650639 } )
651640
652641 utils . addProperty ( chai . Assertion . prototype , 'resolves' , function __VITEST_RESOLVES__ ( this : any ) {
642+ const error = new Error ( 'resolves' )
653643 utils . flag ( this , 'promise' , 'resolves' )
654- utils . flag ( this , 'error' , new Error ( 'resolves' ) )
644+ utils . flag ( this , 'error' , error )
655645 const test : Test = utils . flag ( this , 'vitest-test' )
656646 const obj = utils . flag ( this , 'object' )
657647
@@ -672,7 +662,12 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
672662 return result . call ( this , ...args )
673663 } ,
674664 ( err : any ) => {
675- throw new Error ( `promise rejected "${ String ( err ) } " instead of resolving` )
665+ const _error = new AssertionError (
666+ `promise rejected "${ utils . inspect ( err ) } " instead of resolving` ,
667+ { showDiff : false } ,
668+ )
669+ _error . stack = ( error . stack as string ) . replace ( error . message , _error . message )
670+ throw _error
676671 } ,
677672 )
678673
@@ -685,8 +680,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
685680 } )
686681
687682 utils . addProperty ( chai . Assertion . prototype , 'rejects' , function __VITEST_REJECTS__ ( this : any ) {
683+ const error = new Error ( 'rejects' )
688684 utils . flag ( this , 'promise' , 'rejects' )
689- utils . flag ( this , 'error' , new Error ( 'rejects' ) )
685+ utils . flag ( this , 'error' , error )
690686 const test : Test = utils . flag ( this , 'vitest-test' )
691687 const obj = utils . flag ( this , 'object' )
692688 const wrapper = typeof obj === 'function' ? obj ( ) : obj // for jest compat
@@ -704,7 +700,12 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
704700 return async ( ...args : any [ ] ) => {
705701 const promise = wrapper . then (
706702 ( value : any ) => {
707- throw new Error ( `promise resolved "${ String ( value ) } " instead of rejecting` )
703+ const _error = new AssertionError (
704+ `promise resolved "${ utils . inspect ( value ) } " instead of rejecting` ,
705+ { showDiff : false } ,
706+ )
707+ _error . stack = ( error . stack as string ) . replace ( error . message , _error . message )
708+ throw _error
708709 } ,
709710 ( err : any ) => {
710711 utils . flag ( this , 'object' , err )
0 commit comments