@@ -484,6 +484,73 @@ describe('jest-expect', () => {
484484 } ) . toThrow ( Error )
485485 } ) . toThrowErrorMatchingInlineSnapshot ( `[AssertionError: expected function to throw an error, but it didn't]` )
486486 } )
487+
488+ it ( 'custom error class' , ( ) => {
489+ class Error1 extends Error { } ;
490+ class Error2 extends Error { } ;
491+
492+ // underlying `toEqual` doesn't require constructor/prototype equality
493+ expect ( ( ) => {
494+ throw new Error1 ( 'hi' )
495+ } ) . toThrowError ( new Error2 ( 'hi' ) )
496+ expect ( new Error1 ( 'hi' ) ) . toEqual ( new Error2 ( 'hi' ) )
497+ expect ( new Error1 ( 'hi' ) ) . not . toStrictEqual ( new Error2 ( 'hi' ) )
498+ } )
499+
500+ it ( 'non Error instance' , ( ) => {
501+ // primitives
502+ expect ( ( ) => {
503+ // eslint-disable-next-line no-throw-literal
504+ throw 42
505+ } ) . toThrow ( 42 )
506+ expect ( ( ) => {
507+ // eslint-disable-next-line no-throw-literal
508+ throw 42
509+ } ) . not . toThrow ( 43 )
510+
511+ expect ( ( ) => {
512+ expect ( ( ) => {
513+ // eslint-disable-next-line no-throw-literal
514+ throw 42
515+ } ) . toThrow ( 43 )
516+ } ) . toThrowErrorMatchingInlineSnapshot ( `[AssertionError: expected a thrown value to equal 43]` )
517+
518+ // deep equality
519+ expect ( ( ) => {
520+ // eslint-disable-next-line no-throw-literal
521+ throw { foo : 'hello world' }
522+ } ) . toThrow ( { foo : expect . stringContaining ( 'hello' ) } )
523+ expect ( ( ) => {
524+ // eslint-disable-next-line no-throw-literal
525+ throw { foo : 'bar' }
526+ } ) . not . toThrow ( { foo : expect . stringContaining ( 'hello' ) } )
527+
528+ expect ( ( ) => {
529+ expect ( ( ) => {
530+ // eslint-disable-next-line no-throw-literal
531+ throw { foo : 'bar' }
532+ } ) . toThrow ( { foo : expect . stringContaining ( 'hello' ) } )
533+ } ) . toThrowErrorMatchingInlineSnapshot ( `[AssertionError: expected a thrown value to equal { foo: StringContaining "hello" }]` )
534+ } )
535+
536+ it ( 'error from different realm' , async ( ) => {
537+ const vm = await import ( 'node:vm' )
538+ const context : any = { }
539+ vm . createContext ( context )
540+ new vm . Script ( 'fn = () => { throw new TypeError("oops") }; globalObject = this' ) . runInContext ( context )
541+ const { fn, globalObject } = context
542+
543+ // constructor
544+ expect ( fn ) . toThrow ( globalObject . TypeError )
545+ expect ( fn ) . not . toThrow ( globalObject . ReferenceError )
546+ expect ( fn ) . not . toThrow ( globalObject . EvalError )
547+
548+ // instance
549+ expect ( fn ) . toThrow ( new globalObject . TypeError ( 'oops' ) )
550+ expect ( fn ) . not . toThrow ( new globalObject . TypeError ( 'message' ) )
551+ expect ( fn ) . not . toThrow ( new globalObject . ReferenceError ( 'oops' ) )
552+ expect ( fn ) . not . toThrow ( new globalObject . EvalError ( 'no way' ) )
553+ } )
487554 } )
488555} )
489556
@@ -1892,9 +1959,8 @@ it('error equality', () => {
18921959 // different class
18931960 const e1 = new MyError ( 'hello' , 'a' )
18941961 const e2 = new YourError ( 'hello' , 'a' )
1895- snapshotError ( ( ) => expect ( e1 ) . toEqual ( e2 ) )
1896- expect ( e1 ) . not . toEqual ( e2 )
1897- expect ( e1 ) . not . toStrictEqual ( e2 ) // toStrictEqual checks constructor already
1962+ snapshotError ( ( ) => expect ( e1 ) . toStrictEqual ( e2 ) )
1963+ expect ( e1 ) . toEqual ( e2 )
18981964 assert . deepEqual ( e1 , e2 )
18991965 nodeAssert . notDeepStrictEqual ( e1 , e2 )
19001966 }
0 commit comments