File tree 3 files changed +30
-2
lines changed
3 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,16 @@ exports[`jest-expect-message should fail with custom message 1`] = `
9
9
Received: <red >false</color >"
10
10
` ;
11
11
12
+ exports [` jest-expect-message should fail with custom message for async test 1` ] = `
13
+ "Custom message:
14
+ hello
15
+
16
+ <dim >expect(</intensity ><red >received</color ><dim >).</intensity >rejects<dim >.</intensity >toBe<dim >(</intensity ><green >expected</color ><dim >) // Object.is equality</intensity >
17
+
18
+ Expected: <green >false</color >
19
+ Received: <red >true</color >"
20
+ ` ;
21
+
12
22
exports [` jest-expect-message should fail without custom message 1` ] = `
13
23
"<dim >expect(</intensity ><red >received</color ><dim >).</intensity >toBeTruthy<dim >()</intensity >
14
24
Original file line number Diff line number Diff line change @@ -5,6 +5,12 @@ describe('jest-expect-message', () => {
5
5
expect ( ( ) => expect ( false , 'Woah this should be false!' ) . toBeTruthy ( ) ) . toThrowErrorMatchingSnapshot ( ) ;
6
6
} ) ;
7
7
8
+ test ( 'should fail with custom message for async test' , async ( ) => {
9
+ await expect (
10
+ async ( ) => await expect ( Promise . reject ( true ) , 'hello' ) . rejects . toBe ( false )
11
+ ) . rejects . toThrowErrorMatchingSnapshot ( ) ;
12
+ } ) ;
13
+
8
14
test ( 'should fail without custom message' , ( ) => {
9
15
expect ( ( ) => expect ( false ) . toBeTruthy ( ) ) . toThrowErrorMatchingSnapshot ( ) ;
10
16
} ) ;
Original file line number Diff line number Diff line change @@ -12,8 +12,20 @@ class JestAssertionError extends Error {
12
12
const wrapMatcher = ( matcher , customMessage , config ) => {
13
13
const newMatcher = ( ...args ) => {
14
14
try {
15
- return matcher ( ...args ) ;
16
- } catch ( error ) {
15
+ const result = matcher ( ...args ) ;
16
+
17
+ if ( result && typeof result . then === 'function' ) {
18
+ return result . catch ( rethrowWithMessage ) . catch ( function handleError ( error ) {
19
+ throw new JestAssertionError ( error . matcherResult , handleError ) ;
20
+ } ) ;
21
+ } else {
22
+ return result ;
23
+ }
24
+ } catch ( e ) {
25
+ rethrowWithMessage ( e ) ;
26
+ }
27
+
28
+ function rethrowWithMessage ( error ) {
17
29
if ( ! error . matcherResult ) {
18
30
throw error ;
19
31
}
You can’t perform that action at this time.
0 commit comments