jest-image-snapshot 6.2.0 → 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [6.3.0](https://github.com/americanexpress/jest-image-snapshot/compare/v6.2.0...v6.3.0) (2023-11-28)
2
+
3
+
4
+ ### Features
5
+
6
+ * Add `runtimeHooksPath` options ([#337](https://github.com/americanexpress/jest-image-snapshot/issues/337)) ([57741a2](https://github.com/americanexpress/jest-image-snapshot/commit/57741a242cd2192c453a87c34fa89c7c35a0763c))
7
+
1
8
  # [6.2.0](https://github.com/americanexpress/jest-image-snapshot/compare/v6.1.1...v6.2.0) (2023-07-25)
2
9
 
3
10
 
package/README.md CHANGED
@@ -124,6 +124,9 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
124
124
  * `dumpDiffToConsole`: (default `false`) Will output base64 string of a diff image to console in case of failed tests (in addition to creating a diff image). This string can be copy-pasted to a browser address string to preview the diff for a failed test.
125
125
  * `dumpInlineDiffToConsole`: (default `false`) Will output the image to the terminal using iTerm's [Inline Images Protocol](https://iterm2.com/documentation-images.html). If the term is not compatible, it does the same thing as `dumpDiffToConsole`.
126
126
  * `allowSizeMismatch`: (default `false`) If set to true, the build will not fail when the screenshots to compare have different sizes.
127
+ * `runtimeHooksPath`: (default `undefined`) This needs to be set to a existing file, like `require.resolve('./runtimeHooksPath.cjs')`. This file can expose a few hooks:
128
+ * `onBeforeWriteToDisc`: before saving any image to the disc, this function will be called (can be used to write EXIF data to images for instance)
129
+ `onBeforeWriteToDisc: (arguments: { buffer: Buffer; destination: string; testPath: string; currentTestName: string }) => Buffer`
127
130
 
128
131
  ```javascript
129
132
  it('should demonstrate this matcher`s usage with a custom pixelmatch config', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jest-image-snapshot",
3
- "version": "6.2.0",
3
+ "version": "6.3.0",
4
4
  "description": "Jest matcher for image comparisons. Most commonly used for visual regression testing.",
5
5
  "main": "src/index.js",
6
6
  "engines": {
@@ -30,7 +30,7 @@
30
30
  "!test-results/**"
31
31
  ],
32
32
  "testMatch": [
33
- "<rootDir>/__tests__/**/*.js"
33
+ "<rootDir>/__tests__/**/*.spec.js"
34
34
  ],
35
35
  "coveragePathIgnorePatterns": [
36
36
  "/node_modules/",
@@ -44,8 +44,8 @@
44
44
  "author": "Andres Escobar <andres.escobar@aexp.com> (https://github.com/anescobar1991)",
45
45
  "license": "Apache-2.0",
46
46
  "devDependencies": {
47
- "@commitlint/cli": "^8.3.5",
48
- "@commitlint/config-conventional": "^8.3.4",
47
+ "@commitlint/cli": "^17.6.5",
48
+ "@commitlint/config-conventional": "^17.7.0",
49
49
  "@semantic-release/changelog": "^5.0.0",
50
50
  "@semantic-release/git": "^9.0.0",
51
51
  "amex-jest-preset": "^6.1.0",
@@ -200,6 +200,38 @@ function composeDiff(options) {
200
200
  return composer;
201
201
  }
202
202
 
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
+
203
235
  function diffImageToSnapshot(options) {
204
236
  const {
205
237
  receivedImageBuffer,
@@ -219,6 +251,9 @@ function diffImageToSnapshot(options) {
219
251
  blur,
220
252
  allowSizeMismatch = false,
221
253
  comparisonMethod = 'pixelmatch',
254
+ testPath,
255
+ currentTestName,
256
+ runtimeHooksPath,
222
257
  } = options;
223
258
 
224
259
  const comparisonFn = comparisonMethod === 'ssim' ? ssimMatch : pixelmatch;
@@ -226,7 +261,13 @@ function diffImageToSnapshot(options) {
226
261
  const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}.png`);
227
262
  if (!fs.existsSync(baselineSnapshotPath)) {
228
263
  fs.mkdirSync(path.dirname(baselineSnapshotPath), { recursive: true });
229
- fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
264
+ writeFileWithHooks({
265
+ pathToFile: baselineSnapshotPath,
266
+ content: receivedImageBuffer,
267
+ runtimeHooksPath,
268
+ testPath,
269
+ currentTestName,
270
+ });
230
271
  result = { added: true };
231
272
  } else {
232
273
  const receivedSnapshotPath = path.join(receivedDir, `${snapshotIdentifier}${receivedPostfix}.png`);
@@ -294,7 +335,13 @@ function diffImageToSnapshot(options) {
294
335
  if (isFailure({ pass, updateSnapshot })) {
295
336
  if (storeReceivedOnFailure) {
296
337
  fs.mkdirSync(path.dirname(receivedSnapshotPath), { recursive: true });
297
- fs.writeFileSync(receivedSnapshotPath, receivedImageBuffer);
338
+ writeFileWithHooks({
339
+ pathToFile: receivedSnapshotPath,
340
+ content: receivedImageBuffer,
341
+ runtimeHooksPath,
342
+ testPath,
343
+ currentTestName,
344
+ });
298
345
  result = { receivedSnapshotPath };
299
346
  }
300
347
 
@@ -320,7 +367,13 @@ function diffImageToSnapshot(options) {
320
367
  // Set filter type to Paeth to avoid expensive auto scanline filter detection
321
368
  // For more information see https://www.w3.org/TR/PNG-Filters.html
322
369
  const pngBuffer = PNG.sync.write(compositeResultImage, { filterType: 4 });
323
- fs.writeFileSync(diffOutputPath, pngBuffer);
370
+ writeFileWithHooks({
371
+ pathToFile: diffOutputPath,
372
+ content: pngBuffer,
373
+ runtimeHooksPath,
374
+ testPath,
375
+ currentTestName,
376
+ });
324
377
 
325
378
  result = {
326
379
  ...result,
@@ -334,7 +387,13 @@ function diffImageToSnapshot(options) {
334
387
  };
335
388
  } else if (shouldUpdate({ pass, updateSnapshot, updatePassedSnapshot })) {
336
389
  fs.mkdirSync(path.dirname(baselineSnapshotPath), { recursive: true });
337
- fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
390
+ writeFileWithHooks({
391
+ pathToFile: baselineSnapshotPath,
392
+ content: receivedImageBuffer,
393
+ runtimeHooksPath,
394
+ testPath,
395
+ currentTestName,
396
+ });
338
397
  result = { updated: true };
339
398
  } else {
340
399
  result = {
package/src/index.js CHANGED
@@ -140,6 +140,7 @@ function configureToMatchImageSnapshot({
140
140
  customReceivedPostfix: commonCustomReceivedPostfix,
141
141
  customDiffDir: commonCustomDiffDir,
142
142
  onlyDiff: commonOnlyDiff = false,
143
+ runtimeHooksPath: commonRuntimeHooksPath = undefined,
143
144
  diffDirection: commonDiffDirection = 'horizontal',
144
145
  noColors: commonNoColors,
145
146
  failureThreshold: commonFailureThreshold = 0,
@@ -160,6 +161,7 @@ function configureToMatchImageSnapshot({
160
161
  customReceivedPostfix = commonCustomReceivedPostfix,
161
162
  customDiffDir = commonCustomDiffDir,
162
163
  onlyDiff = commonOnlyDiff,
164
+ runtimeHooksPath = commonRuntimeHooksPath,
163
165
  diffDirection = commonDiffDirection,
164
166
  customDiffConfig = {},
165
167
  noColors = commonNoColors,
@@ -224,6 +226,8 @@ function configureToMatchImageSnapshot({
224
226
  receivedPostfix,
225
227
  diffDir,
226
228
  diffDirection,
229
+ testPath,
230
+ currentTestName,
227
231
  onlyDiff,
228
232
  snapshotIdentifier,
229
233
  updateSnapshot: snapshotState._updateSnapshot === 'all',
@@ -234,6 +238,7 @@ function configureToMatchImageSnapshot({
234
238
  blur,
235
239
  allowSizeMismatch,
236
240
  comparisonMethod,
241
+ runtimeHooksPath,
237
242
  });
238
243
 
239
244
  return checkResult({