1
- import { AssertionError } from 'chai'
2
1
import { assertTypes , getColors } from '@vitest/utils'
3
2
import type { Constructable } from '@vitest/utils'
4
3
import type { EnhancedSpy } from '@vitest/spy'
@@ -13,6 +12,7 @@ import { recordAsyncExpect, wrapSoft } from './utils'
13
12
14
13
// Jest Expect Compact
15
14
export const JestChaiExpect : ChaiPlugin = ( chai , utils ) => {
15
+ const { AssertionError } = chai
16
16
const c = ( ) => getColors ( )
17
17
18
18
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) => {
436
436
if ( called && isNot )
437
437
msg = formatCalls ( spy , msg )
438
438
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 )
444
441
} )
445
442
def ( [ 'toHaveBeenCalledWith' , 'toBeCalledWith' ] , function ( ...args ) {
446
443
const spy = getSpy ( this )
447
444
const spyName = spy . getMockName ( )
448
445
const pass = spy . mock . calls . some ( callArg => jestEquals ( callArg , args , [ iterableEquality ] ) )
449
446
const isNot = utils . flag ( this , 'negate' ) as boolean
450
447
451
- let msg = utils . getMessage (
448
+ const msg = utils . getMessage (
452
449
this ,
453
450
[
454
451
pass ,
@@ -458,12 +455,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
458
455
] ,
459
456
)
460
457
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 ) )
467
460
} )
468
461
def ( [ 'toHaveBeenNthCalledWith' , 'nthCalledWith' ] , function ( times : number , ...args : any [ ] ) {
469
462
const spy = getSpy ( this )
@@ -595,7 +588,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
595
588
const pass = spy . mock . results . some ( ( { type, value : result } ) => type === 'return' && jestEquals ( value , result ) )
596
589
const isNot = utils . flag ( this , 'negate' ) as boolean
597
590
598
- let msg = utils . getMessage (
591
+ const msg = utils . getMessage (
599
592
this ,
600
593
[
601
594
pass ,
@@ -605,12 +598,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
605
598
] ,
606
599
)
607
600
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 ) )
614
603
} )
615
604
def ( [ 'toHaveLastReturnedWith' , 'lastReturnedWith' ] , function ( value : any ) {
616
605
const spy = getSpy ( this )
@@ -650,8 +639,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
650
639
} )
651
640
652
641
utils . addProperty ( chai . Assertion . prototype , 'resolves' , function __VITEST_RESOLVES__ ( this : any ) {
642
+ const error = new Error ( 'resolves' )
653
643
utils . flag ( this , 'promise' , 'resolves' )
654
- utils . flag ( this , 'error' , new Error ( 'resolves' ) )
644
+ utils . flag ( this , 'error' , error )
655
645
const test : Test = utils . flag ( this , 'vitest-test' )
656
646
const obj = utils . flag ( this , 'object' )
657
647
@@ -672,7 +662,12 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
672
662
return result . call ( this , ...args )
673
663
} ,
674
664
( 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
676
671
} ,
677
672
)
678
673
@@ -685,8 +680,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
685
680
} )
686
681
687
682
utils . addProperty ( chai . Assertion . prototype , 'rejects' , function __VITEST_REJECTS__ ( this : any ) {
683
+ const error = new Error ( 'rejects' )
688
684
utils . flag ( this , 'promise' , 'rejects' )
689
- utils . flag ( this , 'error' , new Error ( 'rejects' ) )
685
+ utils . flag ( this , 'error' , error )
690
686
const test : Test = utils . flag ( this , 'vitest-test' )
691
687
const obj = utils . flag ( this , 'object' )
692
688
const wrapper = typeof obj === 'function' ? obj ( ) : obj // for jest compat
@@ -704,7 +700,12 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
704
700
return async ( ...args : any [ ] ) => {
705
701
const promise = wrapper . then (
706
702
( 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
708
709
} ,
709
710
( err : any ) => {
710
711
utils . flag ( this , 'object' , err )
0 commit comments