jest-image-snapshot 6.1.1 → 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,17 @@
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
+
8
+ # [6.2.0](https://github.com/americanexpress/jest-image-snapshot/compare/v6.1.1...v6.2.0) (2023-07-25)
9
+
10
+
11
+ ### Features
12
+
13
+ * allow configuration of postfix for received screenshots filename ([#328](https://github.com/americanexpress/jest-image-snapshot/issues/328)) ([bade294](https://github.com/americanexpress/jest-image-snapshot/commit/bade294ec2843c62b1dbcbf894faffd3a5708b98))
14
+
1
15
  ## [6.1.1](https://github.com/americanexpress/jest-image-snapshot/compare/v6.1.0...v6.1.1) (2023-07-25)
2
16
 
3
17
 
package/CONTRIBUTING.md CHANGED
@@ -47,7 +47,7 @@ Verifies that your code matches the American Express code style defined in [`esl
47
47
 
48
48
  Runs unit tests **and** verifies the format of all commit messages on the current branch.
49
49
 
50
- - **`npm posttest`**
50
+ - **`npm run posttest`**
51
51
 
52
52
  Runs linting on the current branch, checks that the commits follow [conventional commits](https://www.conventionalcommits.org/) and verifies that the `package-lock.json` file includes public NPM registry URLs.
53
53
 
package/README.md CHANGED
@@ -111,6 +111,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
111
111
  * `customDiffDir`: A custom absolute path of a directory to keep this diff in
112
112
  * `storeReceivedOnFailure`: (default: `false`) Store the received images seperately from the composed diff images on failure. This can be useful when updating baseline images from CI.
113
113
  * `customReceivedDir`: A custom absolute path of a directory to keep this received image in
114
+ * `customReceivedPostfix`: A custom postfix which is added to the snapshot name of the received image, defaults to `-received`
114
115
  * `customSnapshotIdentifier`: A custom name to give this snapshot. If not provided one is computed automatically. When a function is provided it is called with an object containing `testPath`, `currentTestName`, `counter` and `defaultIdentifier` as its first argument. The function must return an identifier to use for the snapshot. If a path is given, the path will be created inside the snapshot/diff directories.
115
116
  * `diffDirection`: (default: `horizontal`) (options `horizontal` or `vertical`) Changes diff image layout direction
116
117
  * `onlyDiff`: (default: `false`) Either only include the difference between the baseline and the received image in the diff image, or include the 3 images (following the direction set by `diffDirection`).
@@ -123,6 +124,9 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
123
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.
124
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`.
125
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`
126
130
 
127
131
  ```javascript
128
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.1.1",
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,12 +200,45 @@ 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,
206
238
  snapshotIdentifier,
207
239
  snapshotsDir,
208
240
  storeReceivedOnFailure,
241
+ receivedPostfix = '-received',
209
242
  receivedDir = path.join(options.snapshotsDir, '__received_output__'),
210
243
  diffDir = path.join(options.snapshotsDir, '__diff_output__'),
211
244
  diffDirection,
@@ -218,6 +251,9 @@ function diffImageToSnapshot(options) {
218
251
  blur,
219
252
  allowSizeMismatch = false,
220
253
  comparisonMethod = 'pixelmatch',
254
+ testPath,
255
+ currentTestName,
256
+ runtimeHooksPath,
221
257
  } = options;
222
258
 
223
259
  const comparisonFn = comparisonMethod === 'ssim' ? ssimMatch : pixelmatch;
@@ -225,10 +261,16 @@ function diffImageToSnapshot(options) {
225
261
  const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}.png`);
226
262
  if (!fs.existsSync(baselineSnapshotPath)) {
227
263
  fs.mkdirSync(path.dirname(baselineSnapshotPath), { recursive: true });
228
- fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
264
+ writeFileWithHooks({
265
+ pathToFile: baselineSnapshotPath,
266
+ content: receivedImageBuffer,
267
+ runtimeHooksPath,
268
+ testPath,
269
+ currentTestName,
270
+ });
229
271
  result = { added: true };
230
272
  } else {
231
- const receivedSnapshotPath = path.join(receivedDir, `${snapshotIdentifier}-received.png`);
273
+ const receivedSnapshotPath = path.join(receivedDir, `${snapshotIdentifier}${receivedPostfix}.png`);
232
274
  rimraf.sync(receivedSnapshotPath);
233
275
 
234
276
  const diffOutputPath = path.join(diffDir, `${snapshotIdentifier}-diff.png`);
@@ -293,7 +335,13 @@ function diffImageToSnapshot(options) {
293
335
  if (isFailure({ pass, updateSnapshot })) {
294
336
  if (storeReceivedOnFailure) {
295
337
  fs.mkdirSync(path.dirname(receivedSnapshotPath), { recursive: true });
296
- fs.writeFileSync(receivedSnapshotPath, receivedImageBuffer);
338
+ writeFileWithHooks({
339
+ pathToFile: receivedSnapshotPath,
340
+ content: receivedImageBuffer,
341
+ runtimeHooksPath,
342
+ testPath,
343
+ currentTestName,
344
+ });
297
345
  result = { receivedSnapshotPath };
298
346
  }
299
347
 
@@ -319,7 +367,13 @@ function diffImageToSnapshot(options) {
319
367
  // Set filter type to Paeth to avoid expensive auto scanline filter detection
320
368
  // For more information see https://www.w3.org/TR/PNG-Filters.html
321
369
  const pngBuffer = PNG.sync.write(compositeResultImage, { filterType: 4 });
322
- fs.writeFileSync(diffOutputPath, pngBuffer);
370
+ writeFileWithHooks({
371
+ pathToFile: diffOutputPath,
372
+ content: pngBuffer,
373
+ runtimeHooksPath,
374
+ testPath,
375
+ currentTestName,
376
+ });
323
377
 
324
378
  result = {
325
379
  ...result,
@@ -333,7 +387,13 @@ function diffImageToSnapshot(options) {
333
387
  };
334
388
  } else if (shouldUpdate({ pass, updateSnapshot, updatePassedSnapshot })) {
335
389
  fs.mkdirSync(path.dirname(baselineSnapshotPath), { recursive: true });
336
- fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
390
+ writeFileWithHooks({
391
+ pathToFile: baselineSnapshotPath,
392
+ content: receivedImageBuffer,
393
+ runtimeHooksPath,
394
+ testPath,
395
+ currentTestName,
396
+ });
337
397
  result = { updated: true };
338
398
  } else {
339
399
  result = {
package/src/index.js CHANGED
@@ -137,8 +137,10 @@ function configureToMatchImageSnapshot({
137
137
  customSnapshotsDir: commonCustomSnapshotsDir,
138
138
  storeReceivedOnFailure: commonStoreReceivedOnFailure = false,
139
139
  customReceivedDir: commonCustomReceivedDir,
140
+ customReceivedPostfix: commonCustomReceivedPostfix,
140
141
  customDiffDir: commonCustomDiffDir,
141
142
  onlyDiff: commonOnlyDiff = false,
143
+ runtimeHooksPath: commonRuntimeHooksPath = undefined,
142
144
  diffDirection: commonDiffDirection = 'horizontal',
143
145
  noColors: commonNoColors,
144
146
  failureThreshold: commonFailureThreshold = 0,
@@ -156,8 +158,10 @@ function configureToMatchImageSnapshot({
156
158
  customSnapshotsDir = commonCustomSnapshotsDir,
157
159
  storeReceivedOnFailure = commonStoreReceivedOnFailure,
158
160
  customReceivedDir = commonCustomReceivedDir,
161
+ customReceivedPostfix = commonCustomReceivedPostfix,
159
162
  customDiffDir = commonCustomDiffDir,
160
163
  onlyDiff = commonOnlyDiff,
164
+ runtimeHooksPath = commonRuntimeHooksPath,
161
165
  diffDirection = commonDiffDirection,
162
166
  customDiffConfig = {},
163
167
  noColors = commonNoColors,
@@ -197,6 +201,7 @@ function configureToMatchImageSnapshot({
197
201
 
198
202
  const snapshotsDir = customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR);
199
203
  const receivedDir = customReceivedDir;
204
+ const receivedPostfix = customReceivedPostfix;
200
205
  const diffDir = customDiffDir;
201
206
  const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}.png`);
202
207
  OutdatedSnapshotReporter.markTouchedFile(baselineSnapshotPath);
@@ -218,8 +223,11 @@ function configureToMatchImageSnapshot({
218
223
  snapshotsDir,
219
224
  storeReceivedOnFailure,
220
225
  receivedDir,
226
+ receivedPostfix,
221
227
  diffDir,
222
228
  diffDirection,
229
+ testPath,
230
+ currentTestName,
223
231
  onlyDiff,
224
232
  snapshotIdentifier,
225
233
  updateSnapshot: snapshotState._updateSnapshot === 'all',
@@ -230,6 +238,7 @@ function configureToMatchImageSnapshot({
230
238
  blur,
231
239
  allowSizeMismatch,
232
240
  comparisonMethod,
241
+ runtimeHooksPath,
233
242
  });
234
243
 
235
244
  return checkResult({