@@ -50,6 +50,14 @@ interface AssertOptions {
5050 rawSnapshot ?: RawSnapshotInfo
5151}
5252
53+ /** Same shape as expect.extend custom matcher result (SyncExpectationResult from @vitest/expect) */
54+ export interface MatchResult {
55+ pass : boolean
56+ message : ( ) => string
57+ actual ?: unknown
58+ expected ?: unknown
59+ }
60+
5361export interface SnapshotClientOptions {
5462 isEqual ?: ( received : unknown , expected : unknown ) => boolean
5563}
@@ -99,7 +107,7 @@ export class SnapshotClient {
99107 return state
100108 }
101109
102- assert ( options : AssertOptions ) : void {
110+ match ( options : AssertOptions ) : MatchResult {
103111 const {
104112 filepath,
105113 name,
@@ -119,37 +127,44 @@ export class SnapshotClient {
119127 }
120128
121129 const snapshotState = this . getSnapshotState ( filepath )
130+ const testName = [ name , ...( message ? [ message ] : [ ] ) ] . join ( ' > ' )
131+
132+ // Probe first so we can mark as checked even on early return
133+ const expectedSnapshot = snapshotState . probeExpectedSnapshot ( {
134+ testName,
135+ testId,
136+ isInline,
137+ inlineSnapshot,
138+ } )
122139
123140 if ( typeof properties === 'object' ) {
124141 if ( typeof received !== 'object' || ! received ) {
142+ expectedSnapshot . markAsChecked ( )
125143 throw new Error (
126144 'Received value must be an object when the matcher has properties' ,
127145 )
128146 }
129147
148+ let propertiesPass : boolean
130149 try {
131- const pass = this . options . isEqual ?.( received , properties ) ?? false
132- // const pass = equals(received, properties, [iterableEquality, subsetEquality])
133- if ( ! pass ) {
134- throw createMismatchError (
135- 'Snapshot properties mismatched' ,
136- snapshotState . expand ,
137- received ,
138- properties ,
139- )
140- }
141- else {
142- received = deepMergeSnapshot ( received , properties )
143- }
150+ propertiesPass = this . options . isEqual ?.( received , properties ) ?? false
144151 }
145- catch ( err : any ) {
146- err . message = errorMessage || 'Snapshot mismatched'
152+ catch ( err ) {
153+ expectedSnapshot . markAsChecked ( )
147154 throw err
148155 }
156+ if ( ! propertiesPass ) {
157+ expectedSnapshot . markAsChecked ( )
158+ return {
159+ pass : false ,
160+ message : ( ) => errorMessage || 'Snapshot properties mismatched' ,
161+ actual : received ,
162+ expected : properties ,
163+ }
164+ }
165+ received = deepMergeSnapshot ( received , properties )
149166 }
150167
151- const testName = [ name , ...( message ? [ message ] : [ ] ) ] . join ( ' > ' )
152-
153168 const { actual, expected, key, pass } = snapshotState . match ( {
154169 testId,
155170 testName,
@@ -160,12 +175,23 @@ export class SnapshotClient {
160175 rawSnapshot,
161176 } )
162177
163- if ( ! pass ) {
178+ return {
179+ pass,
180+ message : ( ) => `Snapshot \`${ key || 'unknown' } \` mismatched` ,
181+ actual : rawSnapshot ? actual : actual ?. trim ( ) ,
182+ expected : rawSnapshot ? expected : expected ?. trim ( ) ,
183+ }
184+ }
185+
186+ assert ( options : AssertOptions ) : void {
187+ const result = this . match ( options )
188+ if ( ! result . pass ) {
189+ const snapshotState = this . getSnapshotState ( options . filepath )
164190 throw createMismatchError (
165- `Snapshot \` ${ key || 'unknown' } \` mismatched` ,
191+ result . message ( ) ,
166192 snapshotState . expand ,
167- rawSnapshot ? actual : actual ?. trim ( ) ,
168- rawSnapshot ? expected : expected ?. trim ( ) ,
193+ result . actual ,
194+ result . expected ,
169195 )
170196 }
171197 }
0 commit comments