jest-image-snapshot 6.2.0 → 6.4.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.4.0](https://github.com/americanexpress/jest-image-snapshot/compare/v6.3.0...v6.4.0) (2023-12-11)
2
+
3
+
4
+ ### Features
5
+
6
+ * add configurable maxBuffer option to runDiffImageToSnapshot ([#344](https://github.com/americanexpress/jest-image-snapshot/issues/344)) ([befad8b](https://github.com/americanexpress/jest-image-snapshot/commit/befad8ba6080be6b0a94d098334ea05258afab2e))
7
+
8
+ # [6.3.0](https://github.com/americanexpress/jest-image-snapshot/compare/v6.2.0...v6.3.0) (2023-11-28)
9
+
10
+
11
+ ### Features
12
+
13
+ * Add `runtimeHooksPath` options ([#337](https://github.com/americanexpress/jest-image-snapshot/issues/337)) ([57741a2](https://github.com/americanexpress/jest-image-snapshot/commit/57741a242cd2192c453a87c34fa89c7c35a0763c))
14
+
1
15
  # [6.2.0](https://github.com/americanexpress/jest-image-snapshot/compare/v6.1.1...v6.2.0) (2023-07-25)
2
16
 
3
17
 
package/README.md CHANGED
@@ -124,6 +124,10 @@ 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
+ * `maxChildProcessBufferSizeInBytes`: (default `10 * 1024 * 1024`) Sets the max number of bytes for stdout/stderr when running `diff-snapshot` in a child process.
128
+ * `runtimeHooksPath`: (default `undefined`) This needs to be set to a existing file, like `require.resolve('./runtimeHooksPath.cjs')`. This file can expose a few hooks:
129
+ * `onBeforeWriteToDisc`: before saving any image to the disc, this function will be called (can be used to write EXIF data to images for instance)
130
+ `onBeforeWriteToDisc: (arguments: { buffer: Buffer; destination: string; testPath: string; currentTestName: string }) => Buffer`
127
131
 
128
132
  ```javascript
129
133
  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.4.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 = {
@@ -362,7 +421,7 @@ function runDiffImageToSnapshot(options) {
362
421
  {
363
422
  input: Buffer.from(serializedInput),
364
423
  stdio: ['pipe', 'inherit', 'inherit', 'pipe'],
365
- maxBuffer: 10 * 1024 * 1024, // 10 MB
424
+ maxBuffer: options.maxChildProcessBufferSizeInBytes,
366
425
  }
367
426
  );
368
427
 
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,
@@ -150,6 +151,10 @@ function configureToMatchImageSnapshot({
150
151
  dumpDiffToConsole: commonDumpDiffToConsole = false,
151
152
  dumpInlineDiffToConsole: commonDumpInlineDiffToConsole = false,
152
153
  allowSizeMismatch: commonAllowSizeMismatch = false,
154
+ // Default to 10 MB instead of node's default 1 MB
155
+ // See https://nodejs.org/api/child_process.html#child_processspawnsynccommand-args-options
156
+ maxChildProcessBufferSizeInBytes:
157
+ commonMaxChildProcessBufferSizeInBytes = 10 * 1024 * 1024, // 10 MB
153
158
  comparisonMethod: commonComparisonMethod = 'pixelmatch',
154
159
  } = {}) {
155
160
  return function toMatchImageSnapshot(received, {
@@ -160,6 +165,7 @@ function configureToMatchImageSnapshot({
160
165
  customReceivedPostfix = commonCustomReceivedPostfix,
161
166
  customDiffDir = commonCustomDiffDir,
162
167
  onlyDiff = commonOnlyDiff,
168
+ runtimeHooksPath = commonRuntimeHooksPath,
163
169
  diffDirection = commonDiffDirection,
164
170
  customDiffConfig = {},
165
171
  noColors = commonNoColors,
@@ -171,6 +177,7 @@ function configureToMatchImageSnapshot({
171
177
  dumpDiffToConsole = commonDumpDiffToConsole,
172
178
  dumpInlineDiffToConsole = commonDumpInlineDiffToConsole,
173
179
  allowSizeMismatch = commonAllowSizeMismatch,
180
+ maxChildProcessBufferSizeInBytes = commonMaxChildProcessBufferSizeInBytes,
174
181
  comparisonMethod = commonComparisonMethod,
175
182
  } = {}) {
176
183
  const {
@@ -224,6 +231,8 @@ function configureToMatchImageSnapshot({
224
231
  receivedPostfix,
225
232
  diffDir,
226
233
  diffDirection,
234
+ testPath,
235
+ currentTestName,
227
236
  onlyDiff,
228
237
  snapshotIdentifier,
229
238
  updateSnapshot: snapshotState._updateSnapshot === 'all',
@@ -233,7 +242,9 @@ function configureToMatchImageSnapshot({
233
242
  updatePassedSnapshot,
234
243
  blur,
235
244
  allowSizeMismatch,
245
+ maxChildProcessBufferSizeInBytes,
236
246
  comparisonMethod,
247
+ runtimeHooksPath,
237
248
  });
238
249
 
239
250
  return checkResult({