@@ -200,6 +200,38 @@ function composeDiff(options) {
200200 return composer ;
201201}
202202
203+ function writeFileWithHooks ( {
204+ pathToFile,
205+ content,
206+ runtimeHooksPath,
207+ testPath,
208+ currentTestName,
209+ } ) {
210+ let finalContent = content ;
211+ if ( runtimeHooksPath ) {
212+ let runtimeHooks ;
213+ try {
214+ // As `diffImageToSnapshot` can be called in a worker, and as we cannot pass a function
215+ // to a worker, we need to use an external file path that can be imported
216+ // eslint-disable-next-line import/no-dynamic-require, global-require
217+ runtimeHooks = require ( runtimeHooksPath ) ;
218+ } catch ( e ) {
219+ throw new Error ( `Couldn't import ${ runtimeHooksPath } : ${ e . message } ` ) ;
220+ }
221+ try {
222+ finalContent = runtimeHooks . onBeforeWriteToDisc ( {
223+ buffer : content ,
224+ destination : pathToFile ,
225+ testPath,
226+ currentTestName,
227+ } ) ;
228+ } catch ( e ) {
229+ throw new Error ( `Couldn't execute onBeforeWriteToDisc: ${ e . message } ` ) ;
230+ }
231+ }
232+ fs . writeFileSync ( pathToFile , finalContent ) ;
233+ }
234+
203235function diffImageToSnapshot ( options ) {
204236 const {
205237 receivedImageBuffer,
@@ -224,38 +256,18 @@ function diffImageToSnapshot(options) {
224256 runtimeHooksPath,
225257 } = options ;
226258
227- const writeFileSync = ( pathToFile , content ) => {
228- let finalContent = content ;
229- if ( runtimeHooksPath ) {
230- let runtimeHooks ;
231- try {
232- // As `diffImageToSnapshot` can be called in a worker, and as we cannot pass a function
233- // to a worker, we need to use an external file path that can be imported
234- // eslint-disable-next-line import/no-dynamic-require, global-require
235- runtimeHooks = require ( runtimeHooksPath ) ;
236- } catch ( e ) {
237- throw new Error ( `Couldn't import ${ runtimeHooksPath } : ${ e . message } ` ) ;
238- }
239- try {
240- finalContent = runtimeHooks . onBeforeWriteToDisc ( {
241- buffer : content ,
242- destination : pathToFile ,
243- testPath,
244- currentTestName,
245- } ) ;
246- } catch ( e ) {
247- throw new Error ( `Couldn't execute onBeforeWriteToDisc: ${ e . message } ` ) ;
248- }
249- }
250- fs . writeFileSync ( pathToFile , finalContent ) ;
251- } ;
252-
253259 const comparisonFn = comparisonMethod === 'ssim' ? ssimMatch : pixelmatch ;
254260 let result = { } ;
255261 const baselineSnapshotPath = path . join ( snapshotsDir , `${ snapshotIdentifier } .png` ) ;
256262 if ( ! fs . existsSync ( baselineSnapshotPath ) ) {
257263 fs . mkdirSync ( path . dirname ( baselineSnapshotPath ) , { recursive : true } ) ;
258- writeFileSync ( baselineSnapshotPath , receivedImageBuffer ) ;
264+ writeFileWithHooks ( {
265+ pathToFile : baselineSnapshotPath ,
266+ content : receivedImageBuffer ,
267+ runtimeHooksPath,
268+ testPath,
269+ currentTestName,
270+ } ) ;
259271 result = { added : true } ;
260272 } else {
261273 const receivedSnapshotPath = path . join ( receivedDir , `${ snapshotIdentifier } ${ receivedPostfix } .png` ) ;
@@ -323,7 +335,13 @@ function diffImageToSnapshot(options) {
323335 if ( isFailure ( { pass, updateSnapshot } ) ) {
324336 if ( storeReceivedOnFailure ) {
325337 fs . mkdirSync ( path . dirname ( receivedSnapshotPath ) , { recursive : true } ) ;
326- writeFileSync ( receivedSnapshotPath , receivedImageBuffer ) ;
338+ writeFileWithHooks ( {
339+ pathToFile : receivedSnapshotPath ,
340+ content : receivedImageBuffer ,
341+ runtimeHooksPath,
342+ testPath,
343+ currentTestName,
344+ } ) ;
327345 result = { receivedSnapshotPath } ;
328346 }
329347
@@ -349,7 +367,13 @@ function diffImageToSnapshot(options) {
349367 // Set filter type to Paeth to avoid expensive auto scanline filter detection
350368 // For more information see https://www.w3.org/TR/PNG-Filters.html
351369 const pngBuffer = PNG . sync . write ( compositeResultImage , { filterType : 4 } ) ;
352- writeFileSync ( diffOutputPath , pngBuffer ) ;
370+ writeFileWithHooks ( {
371+ pathToFile : diffOutputPath ,
372+ content : pngBuffer ,
373+ runtimeHooksPath,
374+ testPath,
375+ currentTestName,
376+ } ) ;
353377
354378 result = {
355379 ...result ,
@@ -363,7 +387,13 @@ function diffImageToSnapshot(options) {
363387 } ;
364388 } else if ( shouldUpdate ( { pass, updateSnapshot, updatePassedSnapshot } ) ) {
365389 fs . mkdirSync ( path . dirname ( baselineSnapshotPath ) , { recursive : true } ) ;
366- writeFileSync ( baselineSnapshotPath , receivedImageBuffer ) ;
390+ writeFileWithHooks ( {
391+ pathToFile : baselineSnapshotPath ,
392+ content : receivedImageBuffer ,
393+ runtimeHooksPath,
394+ testPath,
395+ currentTestName,
396+ } ) ;
367397 result = { updated : true } ;
368398 } else {
369399 result = {
0 commit comments