@@ -27,6 +27,7 @@ function getMatcherState(
2727 const obj = assertion . _obj
2828 const isNot = util . flag ( assertion , 'negate' ) as boolean
2929 const promise = util . flag ( assertion , 'promise' ) || ''
30+ const customMessage = util . flag ( assertion , 'message' ) as string | undefined
3031 const jestUtils = {
3132 ...getMatcherUtils ( ) ,
3233 diff,
@@ -52,6 +53,7 @@ function getMatcherState(
5253 state : matcherState ,
5354 isNot,
5455 obj,
56+ customMessage,
5557 }
5658}
5759
@@ -73,7 +75,7 @@ function JestExtendPlugin(
7375 this : Chai . AssertionStatic & Chai . Assertion ,
7476 ...args : any [ ]
7577 ) {
76- const { state, isNot, obj } = getMatcherState ( this , expect )
78+ const { state, isNot, obj, customMessage } = getMatcherState ( this , expect )
7779
7880 const result = expectAssertion . call ( state , obj , ...args )
7981
@@ -85,15 +87,21 @@ function JestExtendPlugin(
8587 const thenable = result as PromiseLike < SyncExpectationResult >
8688 return thenable . then ( ( { pass, message, actual, expected } ) => {
8789 if ( ( pass && isNot ) || ( ! pass && ! isNot ) ) {
88- throw new JestExtendError ( message ( ) , actual , expected )
90+ const errorMessage = customMessage != null
91+ ? customMessage
92+ : message ( )
93+ throw new JestExtendError ( errorMessage , actual , expected )
8994 }
9095 } )
9196 }
9297
9398 const { pass, message, actual, expected } = result as SyncExpectationResult
9499
95100 if ( ( pass && isNot ) || ( ! pass && ! isNot ) ) {
96- throw new JestExtendError ( message ( ) , actual , expected )
101+ const errorMessage = customMessage != null
102+ ? `${ customMessage } : ${ message ( ) } `
103+ : message ( )
104+ throw new JestExtendError ( errorMessage , actual , expected )
97105 }
98106 }
99107
0 commit comments