@@ -7,22 +7,18 @@ export function toMatchInlineSnapshot(
77 actual ,
88 expected ,
99) {
10- const normalizedActual = stripAddedLinebreaks ( stripAddedIndentation ( actual . snapshot ?? actual ) )
10+ const normalizedActual = stripAddedLinebreaks ( stripAddedIndentation ( String ( actual ? .snapshot ?? actual ) ) )
1111 const normalizedExpected = stripAddedLinebreaks ( stripAddedIndentation ( expected ) )
1212
1313 return {
1414 pass : normalizedActual === normalizedExpected ,
1515 message : ( ) => [
16- this . utils . matcherHint (
17- `${ this . isNot ? '.not' : '' } .toMatchInlineSnapshot` ,
18- 'element' ,
19- '' ,
20- ) ,
16+ this . utils . matcherHint ( 'toMatchInlineSnapshot' , undefined , undefined , {
17+ isNot : this . isNot ,
18+ promise : this . promise ,
19+ } ) ,
2120 '' ,
22- `Expected: ${ this . isNot ? 'not ' : '' } ${ this . promise } ` ,
23- ` ${ this . utils . printExpected ( normalizedExpected ) } ` ,
24- 'Received:' ,
25- ` ${ this . utils . printReceived ( normalizedActual ) } ` ,
21+ this . utils . diff ( normalizedExpected , normalizedActual , { expand : this . expand } ) ,
2622 ] . join ( '\n' ) ,
2723 }
2824}
@@ -31,30 +27,44 @@ export function toThrowErrorMatchingInlineSnapshot(
3127 callback ,
3228 expected ,
3329) {
34- let didThrow = false , actual = undefined
35- try {
36- callback ( )
37- } catch ( e ) {
38- didThrow = true
39- actual = e
30+ if ( typeof callback === 'function' ) {
31+ try {
32+ const promise = callback ( )
33+ if ( typeof promise === 'object' && 'then' in promise ) {
34+ return promise
35+ . then ( ( ) => ( { didThrow : false } ) , r => ( { didThrow : true , actual : r } ) )
36+ . then ( ( { didThrow, actual } ) => matchError . call ( this , didThrow , actual , expected ) )
37+ }
38+ } catch ( e ) {
39+ return matchError . call ( this , true , e , expected )
40+ }
41+ return matchError . call ( this , false , undefined , expected )
42+ } else if ( this . promise === 'rejects' ) {
43+ return matchError . call ( this , true , callback , expected )
4044 }
4145
46+ throw new Error ( 'Invalid argument' )
47+ }
48+
49+ function matchError (
50+ didThrow ,
51+ actual ,
52+ expected ,
53+ ) {
4254 const normalizedActual = didThrow && stripAddedLinebreaks ( stripAddedIndentation ( typeof actual === 'object' && 'message' in actual ? actual . message : String ( actual ) ) )
4355 const normalizedExpected = stripAddedLinebreaks ( stripAddedIndentation ( expected ) )
4456
4557 return {
4658 pass : this . isNot === ! didThrow && normalizedActual === normalizedExpected ,
4759 message : ( ) => [
48- this . utils . matcherHint (
49- `${ this . isNot ? '.not' : '' } .toThrowErrorMatchingInlineSnapshot` ,
50- 'callback' ,
51- '' ,
52- ) ,
60+ this . utils . matcherHint ( 'toThrowErrorMatchingInlineSnapshot' , undefined , undefined , {
61+ isNot : this . isNot ,
62+ promise : this . promise ,
63+ } ) ,
5364 '' ,
54- `Expected: ${ this . isNot ? 'not ' : '' } ${ this . promise } ` ,
55- ` ${ this . utils . printExpected ( normalizedExpected ) } ` ,
56- 'Received:' ,
57- ` ${ didThrow ? this . utils . printReceived ( normalizedActual ) : '[Did not throw]' } ` ,
65+ didThrow
66+ ? this . utils . diff ( normalizedExpected , normalizedActual , { expand : this . expand } )
67+ : '[Did not throw]' ,
5868 ] . join ( '\n' ) ,
5969 }
6070}
0 commit comments