@@ -13,7 +13,8 @@ declare global {
1313 // eslint-disable-next-line @typescript-eslint/no-namespace
1414 namespace jest {
1515 interface Matchers < R > {
16- toThrowStreamrError ( expectedError : PartialStreamrClientError ) : R
16+ toThrowStreamrClientError ( expectedError : PartialStreamrClientError ) : R
17+ toEqualStreamrClientError ( expectedError : PartialStreamrClientError ) : R
1718 }
1819 }
1920}
@@ -22,7 +23,7 @@ const formErrorMessage = (field: keyof StreamrClientError, expected: string, act
2223 return `StreamrClientError ${ field } values don't match:\nExpected: ${ printExpected ( expected ) } \nReceived: ${ printReceived ( actual ) } `
2324}
2425
25- const toThrowStreamrError = (
26+ const toThrowStreamrClientError = (
2627 actual : unknown , // should be (() => StreamrClientError) | StreamrClientError
2728 expectedError : PartialStreamrClientError
2829) : jest . CustomMatcherResult => {
@@ -40,32 +41,52 @@ const toThrowStreamrError = (
4041 } else {
4142 actualError = actual
4243 }
44+ const assertionErrors = createAssertionErrors ( actualError , expectedError )
45+ return toCustomMatcherResult ( assertionErrors , 'Expected not to throw StreamrClientError' )
46+ }
47+
48+ const toEqualStreamrClientError = (
49+ actual : unknown , // should be StreamrClientError
50+ expectedError : PartialStreamrClientError
51+ ) : jest . CustomMatcherResult => {
52+ const assertionErrors = createAssertionErrors ( actual , expectedError )
53+ return toCustomMatcherResult ( assertionErrors , 'StreamrClientErrors are equal' )
54+ }
4355
44- const messages : string [ ] = [ ]
56+ const createAssertionErrors = (
57+ actualError : unknown ,
58+ expectedError : PartialStreamrClientError
59+ ) : string [ ] => {
60+ const assertionErrors : string [ ] = [ ]
4561 if ( ! ( actualError instanceof StreamrClientError ) ) {
4662 const received = isObject ( actualError ) ? actualError . constructor . name : actualError
47- messages . push ( `Not an instance of StreamrClientError:\nReceived: ${ printReceived ( received ) } ` )
63+ assertionErrors . push ( `Not an instance of StreamrClientError:\nReceived: ${ printReceived ( received ) } ` )
4864 } else {
4965 if ( actualError . code !== expectedError . code ) {
50- messages . push ( formErrorMessage ( 'code' , expectedError . code , actualError . code ) )
66+ assertionErrors . push ( formErrorMessage ( 'code' , expectedError . code , actualError . code ) )
5167 }
5268 if ( ( expectedError . message !== undefined ) && ( actualError . message !== expectedError . message ) ) {
53- messages . push ( formErrorMessage ( 'message' , expectedError . message , actualError . message ) )
69+ assertionErrors . push ( formErrorMessage ( 'message' , expectedError . message , actualError . message ) )
5470 }
5571 }
56- if ( messages . length > 0 ) {
72+ return assertionErrors
73+ }
74+
75+ const toCustomMatcherResult = ( assertionErrors : string [ ] , inversionErrorMessage : string ) => {
76+ if ( assertionErrors . length > 0 ) {
5777 return {
5878 pass : false ,
59- message : ( ) => messages . join ( '\n\n' )
79+ message : ( ) => assertionErrors . join ( '\n\n' )
6080 }
6181 } else {
6282 return {
6383 pass : true ,
64- message : ( ) => `Expected not to throw StreamrClientError}`
84+ message : ( ) => inversionErrorMessage
6585 }
6686 }
6787}
6888
6989expect . extend ( {
70- toThrowStreamrError
90+ toThrowStreamrClientError,
91+ toEqualStreamrClientError
7192} )
0 commit comments