@@ -20,10 +20,16 @@ function returnValues(value: SlapshotDataFormat) {
2020 }
2121}
2222
23+ type ValidationCallback = ( liveData : any , snapshottedData : any ) => void ;
24+ type ValidationOptions = boolean | ValidationCallback ;
25+
2326export function memorize < ReturnedData = any > (
2427 snapshotName : string ,
2528 method : ( ) => Promise < ReturnedData > | ReturnedData ,
26- { pure = true } : { pure ?: boolean } = { }
29+ {
30+ pure = true ,
31+ validateSnapshot = false
32+ } : { pure ?: boolean ; validateSnapshot ?: ValidationOptions } = { }
2733) : Promise < ReturnedData > | ReturnedData {
2834 let jestContext : any ;
2935 // Hack to get access to jest current test context
@@ -97,7 +103,8 @@ export function memorize<ReturnedData = any>(
97103 results : null
98104 } ,
99105 jestContext ,
100- snapshots
106+ snapshots ,
107+ validateSnapshot
101108 ) ;
102109 } )
103110 . then ( methodResults => {
@@ -109,7 +116,8 @@ export function memorize<ReturnedData = any>(
109116 results : methodResults
110117 } ,
111118 jestContext ,
112- snapshots
119+ snapshots ,
120+ validateSnapshot
113121 ) ;
114122 } ) ;
115123 }
@@ -120,7 +128,8 @@ export function memorize<ReturnedData = any>(
120128 fullSnapshotName ,
121129 methodResults ,
122130 jestContext ,
123- snapshots
131+ snapshots ,
132+ validateSnapshot
124133 ) ;
125134}
126135
@@ -130,36 +139,46 @@ function resolveData(
130139 fullSnapshotName : string ,
131140 methodResults : SlapshotDataFormat ,
132141 jestContext : any ,
133- snapshots : Snapshot
142+ snapshots : Snapshot ,
143+ validateSnapshot : ValidationOptions
134144) {
135145 if ( ! shouldUpdateSnapshot ( ) && runInOnlineMode ( ) ) {
136146 let snapDataToCompare = snap ;
137- if ( typeof snapDataToCompare === "object" ) {
138- snapDataToCompare = JSON . stringify ( snapDataToCompare ) ;
139- }
140-
141- let methodResultsToCompare : any = methodResults ;
142- if ( typeof methodResultsToCompare === "object" ) {
143- methodResultsToCompare = JSON . stringify ( methodResultsToCompare ) ;
144- }
145147
146- if (
147- ( snap . results || snap . thrownError ) &&
148- methodResultsToCompare !== snapDataToCompare
149- ) {
150- throw new Error (
151- `[Warning] Intigration test result does not match the memorized snap file:
148+ if ( validateSnapshot && typeof validateSnapshot === "function" ) {
149+ validateSnapshot ( methodResults , snapDataToCompare ) ;
150+ } else {
151+ if ( typeof snapDataToCompare === "object" ) {
152+ snapDataToCompare = JSON . stringify ( snapDataToCompare ) ;
153+ }
154+
155+ let methodResultsToCompare : any = methodResults ;
156+ if ( typeof methodResultsToCompare === "object" ) {
157+ methodResultsToCompare = JSON . stringify ( methodResultsToCompare ) ;
158+ }
159+
160+ if (
161+ ( snap . results || snap . thrownError ) &&
162+ methodResultsToCompare !== snapDataToCompare
163+ ) {
164+ const defaultWarning = `[Warning] Integration test result does not match the memorized snap file:
152165 - Method snapshot name: ${ fullSnapshotName }
153166 - Test file: ${ jestContext . testPath }
154167 - Live result: ${ methodResultsToCompare }
155168 - Existing Snap: ${ snapDataToCompare }
156-
169+
157170 ${ process . env . SLAPSHOT_RERUN_MESSAGE ||
158171 "Please re-run Jest with the --updateSnapshot flag AND the env var SLAPSHOT_ONLINE=true" } .`. replace (
159- new RegExp ( " " , "g" ) ,
172+ new RegExp ( " " , "g" ) ,
160173 ""
161- )
162- ) ;
174+ ) ;
175+
176+ if ( ! validateSnapshot ) {
177+ console . warn ( defaultWarning ) ;
178+ } else if ( typeof validateSnapshot === "boolean" ) {
179+ throw new Error ( defaultWarning ) ;
180+ }
181+ }
163182 }
164183
165184 return returnValues ( methodResults ) ;
0 commit comments