Skip to content

Commit befad8b

Browse files
authored
feat: add configurable maxBuffer option to runDiffImageToSnapshot (#344)
* feat: add configurable maxBuffer option to runDiffImageToSnapshot * fixup! feat: add configurable maxBuffer option to runDiffImageToSnapshot
1 parent 7b1e622 commit befad8b

File tree

6 files changed

+13
-1
lines changed

6 files changed

+13
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
124124
* `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.
125125
* `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`.
126126
* `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.
127128
* `runtimeHooksPath`: (default `undefined`) This needs to be set to a existing file, like `require.resolve('./runtimeHooksPath.cjs')`. This file can expose a few hooks:
128129
* `onBeforeWriteToDisc`: before saving any image to the disc, this function will be called (can be used to write EXIF data to images for instance)
129130
`onBeforeWriteToDisc: (arguments: { buffer: Buffer; destination: string; testPath: string; currentTestName: string }) => Buffer`
456 KB
Loading

__tests__/__snapshots__/index.spec.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to
4848
"diffDirection": "horizontal",
4949
"failureThreshold": 0,
5050
"failureThresholdType": "pixel",
51+
"maxChildProcessBufferSizeInBytes": 10485760,
5152
"onlyDiff": false,
5253
"receivedDir": undefined,
5354
"receivedImageBuffer": "pretendthisisanimagebuffer",

__tests__/index.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ describe('toMatchImageSnapshot', () => {
448448
onlyDiff: false,
449449
failureThreshold: 0,
450450
failureThresholdType: 'pixel',
451+
maxChildProcessBufferSizeInBytes: 10 * 1024 * 1024,
451452
receivedDir: undefined,
452453
receivedImageBuffer: undefined,
453454
runtimeHooksPath: undefined,
@@ -500,6 +501,7 @@ describe('toMatchImageSnapshot', () => {
500501
failureThresholdType: 'percent',
501502
updatePassedSnapshot: true,
502503
blur: 1,
504+
maxChildProcessBufferSizeInBytes: 1024 * 1024,
503505
comparisonMethod,
504506
});
505507
expect.extend({ toMatchImageSnapshot });
@@ -528,6 +530,7 @@ describe('toMatchImageSnapshot', () => {
528530
updatePassedSnapshot: true,
529531
failureThreshold: 1,
530532
failureThresholdType: 'percent',
533+
maxChildProcessBufferSizeInBytes: 1024 * 1024,
531534
comparisonMethod,
532535
});
533536
expect(Chalk).toHaveBeenCalledWith({
@@ -590,6 +593,7 @@ describe('toMatchImageSnapshot', () => {
590593
testPath: path.join('path', 'to', 'test.spec.js'),
591594
updateSnapshot: false,
592595
updatePassedSnapshot: false,
596+
maxChildProcessBufferSizeInBytes: 10 * 1024 * 1024,
593597
failureThreshold: 0,
594598
failureThresholdType: 'pixel',
595599
comparisonMethod: 'pixelmatch',

src/diff-snapshot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ function runDiffImageToSnapshot(options) {
421421
{
422422
input: Buffer.from(serializedInput),
423423
stdio: ['pipe', 'inherit', 'inherit', 'pipe'],
424-
maxBuffer: 10 * 1024 * 1024, // 10 MB
424+
maxBuffer: options.maxChildProcessBufferSizeInBytes,
425425
}
426426
);
427427

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ function configureToMatchImageSnapshot({
151151
dumpDiffToConsole: commonDumpDiffToConsole = false,
152152
dumpInlineDiffToConsole: commonDumpInlineDiffToConsole = false,
153153
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
154158
comparisonMethod: commonComparisonMethod = 'pixelmatch',
155159
} = {}) {
156160
return function toMatchImageSnapshot(received, {
@@ -173,6 +177,7 @@ function configureToMatchImageSnapshot({
173177
dumpDiffToConsole = commonDumpDiffToConsole,
174178
dumpInlineDiffToConsole = commonDumpInlineDiffToConsole,
175179
allowSizeMismatch = commonAllowSizeMismatch,
180+
maxChildProcessBufferSizeInBytes = commonMaxChildProcessBufferSizeInBytes,
176181
comparisonMethod = commonComparisonMethod,
177182
} = {}) {
178183
const {
@@ -237,6 +242,7 @@ function configureToMatchImageSnapshot({
237242
updatePassedSnapshot,
238243
blur,
239244
allowSizeMismatch,
245+
maxChildProcessBufferSizeInBytes,
240246
comparisonMethod,
241247
runtimeHooksPath,
242248
});

0 commit comments

Comments
 (0)