@@ -43,7 +43,7 @@ interface AssertOptions {
4343 error ?: Error
4444 errorMessage ?: string
4545 rawSnapshot ?: RawSnapshotInfo
46- assertionName ? : string
46+ assertionName : string
4747}
4848
4949interface AssertDomainOptions extends Omit < AssertOptions , 'received' > {
@@ -63,8 +63,19 @@ export interface MatchResult {
6363 message : ( ) => string
6464 actual ?: unknown
6565 expected ?: unknown
66+ /** @internal */
67+ meta ?: MatchMeta
6668}
6769
70+ /**
71+ * Information about the kind of processed snapshot and data useful for constructing an update
72+ */
73+ export type MatchMeta = { type : 'snapshot' ; assertionName : string ; key : string } & (
74+ | { kind : 'file' ; file : string ; renderedContent ?: string }
75+ | { kind : 'inline' ; renderedContent ?: string }
76+ | { kind : 'raw' ; file : string }
77+ )
78+
6879interface SnapshotClientOptions {
6980 isEqual ?: ( received : unknown , expected : unknown ) => boolean
7081}
@@ -167,6 +178,12 @@ export class SnapshotClient {
167178 message : ( ) => errorMessage || 'Snapshot properties mismatched' ,
168179 actual : received ,
169180 expected : properties ,
181+ meta : createMetaObject ( {
182+ assertionName,
183+ isInline,
184+ rawSnapshot,
185+ snapshotState,
186+ } ) ,
170187 }
171188 }
172189 received = deepMergeSnapshot ( received , properties )
@@ -188,6 +205,13 @@ export class SnapshotClient {
188205 message : ( ) => `Snapshot \`${ key || 'unknown' } \` mismatched` ,
189206 actual : rawSnapshot ? actual : actual ?. trim ( ) ,
190207 expected : rawSnapshot ? expected : expected ?. trim ( ) ,
208+ meta : createMetaObject ( {
209+ assertionName,
210+ isInline,
211+ key,
212+ rawSnapshot,
213+ snapshotState,
214+ } ) ,
191215 }
192216 }
193217
@@ -215,6 +239,7 @@ export class SnapshotClient {
215239 isInline = false ,
216240 inlineSnapshot,
217241 error,
242+ assertionName,
218243 } = options
219244
220245 if ( ! filepath ) {
@@ -245,14 +270,21 @@ export class SnapshotClient {
245270 matchResult,
246271 isInline,
247272 error,
248- assertionName : options . assertionName ,
273+ assertionName,
249274 } )
250275
251276 return {
252277 pass,
253278 message : ( ) => `Snapshot \`${ key } \` mismatched` ,
254279 actual : actual ?. trim ( ) ,
255280 expected : expected ?. trim ( ) ,
281+ meta : createMetaObject ( {
282+ rendered,
283+ assertionName,
284+ isInline,
285+ key,
286+ snapshotState,
287+ } ) ,
256288 }
257289 }
258290
@@ -269,6 +301,7 @@ export class SnapshotClient {
269301 error,
270302 timeout = 1000 ,
271303 interval = 50 ,
304+ assertionName,
272305 } = options
273306
274307 if ( ! filepath ) {
@@ -335,14 +368,21 @@ export class SnapshotClient {
335368 matchResult,
336369 isInline,
337370 error,
338- assertionName : options . assertionName ,
371+ assertionName,
339372 } )
340373
341374 return {
342375 pass,
343376 message : ( ) => `Snapshot \`${ key } \` mismatched` ,
344377 actual : actual ?. trim ( ) ,
345378 expected : expected ?. trim ( ) ,
379+ meta : createMetaObject ( {
380+ rendered : stableResult . rendered ,
381+ assertionName,
382+ isInline,
383+ key,
384+ snapshotState,
385+ } ) ,
346386 }
347387 }
348388
@@ -446,3 +486,52 @@ function raceWith<A, B>(
446486 }
447487 return Promise . race ( [ left , other . then ( ( value ) => ( { ok : false as const , value } ) ) ] )
448488}
489+
490+ interface CreateMetaOptions extends Pick <
491+ AssertOptions ,
492+ 'assertionName' | 'isInline' | 'rawSnapshot'
493+ > {
494+ rendered ?: string
495+ snapshotState : SnapshotState
496+ key ?: string
497+ }
498+
499+ function createMetaObject ( {
500+ rendered,
501+ assertionName,
502+ isInline,
503+ key,
504+ rawSnapshot,
505+ snapshotState,
506+ } : CreateMetaOptions ) : MatchMeta {
507+ const k = key ?? 'unknown'
508+
509+ if ( isInline ) {
510+ return {
511+ type : 'snapshot' ,
512+ kind : 'inline' ,
513+ assertionName,
514+ key : k ,
515+ renderedContent : rendered ,
516+ }
517+ }
518+
519+ if ( rawSnapshot ) {
520+ return {
521+ type : 'snapshot' ,
522+ kind : 'raw' ,
523+ assertionName,
524+ file : rawSnapshot . file ,
525+ key : k ,
526+ }
527+ }
528+
529+ return {
530+ type : 'snapshot' ,
531+ kind : 'file' ,
532+ assertionName,
533+ file : snapshotState . snapshotPath ,
534+ key : k ,
535+ renderedContent : rendered ,
536+ }
537+ }
0 commit comments