Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add configurable maxBuffer option to runDiffImageToSnapshot #344

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
* `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.
* `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`.
* `allowSizeMismatch`: (default `false`) If set to true, the build will not fail when the screenshots to compare have different sizes.
* `maxChildProcessBufferSizeInBytes`: (default `10 * 1024 * 1024`) Sets the max number of bytes for stdout/stderr when running `diff-snapshot` in a child process.
* `runtimeHooksPath`: (default `undefined`) This needs to be set to a existing file, like `require.resolve('./runtimeHooksPath.cjs')`. This file can expose a few hooks:
* `onBeforeWriteToDisc`: before saving any image to the disc, this function will be called (can be used to write EXIF data to images for instance)
`onBeforeWriteToDisc: (arguments: { buffer: Buffer; destination: string; testPath: string; currentTestName: string }) => Buffer`
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to
"diffDirection": "horizontal",
"failureThreshold": 0,
"failureThresholdType": "pixel",
"maxChildProcessBufferSizeInBytes": 10485760,
"onlyDiff": false,
"receivedDir": undefined,
"receivedImageBuffer": "pretendthisisanimagebuffer",
Expand Down
4 changes: 4 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ describe('toMatchImageSnapshot', () => {
onlyDiff: false,
failureThreshold: 0,
failureThresholdType: 'pixel',
maxChildProcessBufferSizeInBytes: 10 * 1024 * 1024,
receivedDir: undefined,
receivedImageBuffer: undefined,
runtimeHooksPath: undefined,
Expand Down Expand Up @@ -500,6 +501,7 @@ describe('toMatchImageSnapshot', () => {
failureThresholdType: 'percent',
updatePassedSnapshot: true,
blur: 1,
maxChildProcessBufferSizeInBytes: 1024 * 1024,
comparisonMethod,
});
expect.extend({ toMatchImageSnapshot });
Expand Down Expand Up @@ -528,6 +530,7 @@ describe('toMatchImageSnapshot', () => {
updatePassedSnapshot: true,
failureThreshold: 1,
failureThresholdType: 'percent',
maxChildProcessBufferSizeInBytes: 1024 * 1024,
comparisonMethod,
});
expect(Chalk).toHaveBeenCalledWith({
Expand Down Expand Up @@ -590,6 +593,7 @@ describe('toMatchImageSnapshot', () => {
testPath: path.join('path', 'to', 'test.spec.js'),
updateSnapshot: false,
updatePassedSnapshot: false,
maxChildProcessBufferSizeInBytes: 10 * 1024 * 1024,
failureThreshold: 0,
failureThresholdType: 'pixel',
comparisonMethod: 'pixelmatch',
Expand Down
2 changes: 1 addition & 1 deletion src/diff-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ function runDiffImageToSnapshot(options) {
{
input: Buffer.from(serializedInput),
stdio: ['pipe', 'inherit', 'inherit', 'pipe'],
maxBuffer: 10 * 1024 * 1024, // 10 MB
maxBuffer: options.maxChildProcessBufferSizeInBytes,
}
);

Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ function configureToMatchImageSnapshot({
dumpDiffToConsole: commonDumpDiffToConsole = false,
dumpInlineDiffToConsole: commonDumpInlineDiffToConsole = false,
allowSizeMismatch: commonAllowSizeMismatch = false,
// Default to 10 MB instead of node's default 1 MB
// See https://nodejs.org/api/child_process.html#child_processspawnsynccommand-args-options
maxChildProcessBufferSizeInBytes:
commonMaxChildProcessBufferSizeInBytes = 10 * 1024 * 1024, // 10 MB
comparisonMethod: commonComparisonMethod = 'pixelmatch',
} = {}) {
return function toMatchImageSnapshot(received, {
Expand All @@ -173,6 +177,7 @@ function configureToMatchImageSnapshot({
dumpDiffToConsole = commonDumpDiffToConsole,
dumpInlineDiffToConsole = commonDumpInlineDiffToConsole,
allowSizeMismatch = commonAllowSizeMismatch,
maxChildProcessBufferSizeInBytes = commonMaxChildProcessBufferSizeInBytes,
comparisonMethod = commonComparisonMethod,
} = {}) {
const {
Expand Down Expand Up @@ -237,6 +242,7 @@ function configureToMatchImageSnapshot({
updatePassedSnapshot,
blur,
allowSizeMismatch,
maxChildProcessBufferSizeInBytes,
comparisonMethod,
runtimeHooksPath,
});
Expand Down
Loading