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 onlyDiff in options #317

Merged
merged 4 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
* `customReceivedDir`: A custom absolute path of a directory to keep this received image in
* `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.
* `diffDirection`: (default: `horizontal`) (options `horizontal` or `vertical`) Changes diff image layout direction
* `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`).
* `noColors`: Removes coloring from console output, useful if storing the results in a file
* `failureThreshold`: (default `0`) Sets the threshold that would trigger a test failure based on the `failureThresholdType` selected. This is different to the `customDiffConfig.threshold` above, that is the per pixel failure threshold, this is the failure threshold for the entire comparison.
* `failureThresholdType`: (default `pixel`) (options `percent` or `pixel`) Sets the type of threshold that would trigger a failure.
Expand Down
1 change: 1 addition & 0 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to
"diffDirection": "horizontal",
"failureThreshold": 0,
"failureThresholdType": "pixel",
"onlyDiff": false,
"receivedDir": undefined,
"receivedImageBuffer": "pretendthisisanimagebuffer",
"snapshotIdentifier": "test-spec-js-test-1-snap",
Expand Down
20 changes: 20 additions & 0 deletions __tests__/diff-snapshot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,26 @@ describe('diff-snapshot', () => {
expect(diffResult).toHaveProperty('diffOutputPath', path.join(mockSnapshotsDir, '__diff_output__', `${mockSnapshotIdentifier}-diff.png`));
});

it('should only generate an image with the diff when onlyDiff is set to true', () => {
const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 250 });
const diffResult = diffImageToSnapshot({
receivedImageBuffer: mockFailImageBuffer,
snapshotIdentifier: mockSnapshotIdentifier,
snapshotsDir: mockSnapshotsDir,
receivedDir: mockReceivedDir,
diffDir: mockDiffDir,
updateSnapshot: false,
failureThreshold: 0,
failureThresholdType: 'pixel',
onlyDiff: true,
});

expect(diffResult).toHaveProperty('pass', false);
// White image without the baseline, nor the received (white because we mock pixelmatch)
expect(diffResult).toHaveProperty('imgSrcString', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAA30lEQVR4Ae3BIQEAAACDMAT9M78G4ptcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcygA9UAGR3m7UvwAAAABJRU5ErkJggg==');
expect(diffResult).toHaveProperty('diffOutputPath', path.join(mockSnapshotsDir, '__diff_output__', `${mockSnapshotIdentifier}-diff.png`));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions don't give me any confidence. Can we add something that clearly checks that only the diff is included?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I add to the stubs folder the expected result of test instead of doing a manual data:image/png;base64,…?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: I removed this test and I moved it to an integration test, so that we can have a real check and a real diff image

});

it('should throw an error if an unknown threshold type is supplied', () => {
const diffImageToSnapshot = setupTest({ snapshotExists: true });

Expand Down
3 changes: 3 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ describe('toMatchImageSnapshot', () => {
comparisonMethod: 'pixelmatch',
customDiffConfig: {},
diffDirection: 'horizontal',
onlyDiff: false,
failureThreshold: 0,
failureThresholdType: 'pixel',
receivedImageBuffer: undefined,
Expand Down Expand Up @@ -513,6 +514,7 @@ describe('toMatchImageSnapshot', () => {
storeReceivedOnFailure: true,
diffDir: path.join('path', 'to', 'my-custom-diff-dir'),
diffDirection: 'vertical',
onlyDiff: false,
updateSnapshot: false,
updatePassedSnapshot: true,
failureThreshold: 1,
Expand Down Expand Up @@ -572,6 +574,7 @@ describe('toMatchImageSnapshot', () => {
receivedDir: path.join('path', 'to', 'my-custom-received-dir'),
diffDir: path.join('path', 'to', 'my-custom-diff-dir'),
diffDirection: 'horizontal',
onlyDiff: false,
storeReceivedOnFailure: true,
updateSnapshot: false,
updatePassedSnapshot: false,
Expand Down
28 changes: 22 additions & 6 deletions src/diff-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,24 @@ const shouldFail = ({
};
};

function composeDiff(options) {
const {
diffDirection, baselineImage, diffImage, receivedImage, imageWidth, imageHeight, onlyDiff,
} = options;
const composer = new ImageComposer({
direction: diffDirection,
});

if (!onlyDiff) {
composer.addImage(baselineImage, imageWidth, imageHeight);
}
composer.addImage(diffImage, imageWidth, imageHeight);
if (!onlyDiff) {
composer.addImage(receivedImage, imageWidth, imageHeight);
}
return composer;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a new function, otherwise the complexity of diffImageToSnapshot was too high

Ayc0 marked this conversation as resolved.
Show resolved Hide resolved
}

function diffImageToSnapshot(options) {
const {
receivedImageBuffer,
Expand All @@ -193,6 +211,7 @@ function diffImageToSnapshot(options) {
receivedDir = path.join(options.snapshotsDir, '__received_output__'),
diffDir = path.join(options.snapshotsDir, '__diff_output__'),
diffDirection,
onlyDiff = false,
updateSnapshot = false,
updatePassedSnapshot = false,
customDiffConfig = {},
Expand Down Expand Up @@ -281,14 +300,10 @@ function diffImageToSnapshot(options) {
}

mkdirp.sync(path.dirname(diffOutputPath));
const composer = new ImageComposer({
direction: diffDirection,
const composer = composeDiff({
diffDirection, baselineImage, diffImage, receivedImage, imageWidth, imageHeight, onlyDiff,
});

composer.addImage(baselineImage, imageWidth, imageHeight);
composer.addImage(diffImage, imageWidth, imageHeight);
composer.addImage(receivedImage, imageWidth, imageHeight);

const composerParams = composer.getParams();

const compositeResultImage = new PNG({
Expand Down Expand Up @@ -335,6 +350,7 @@ function diffImageToSnapshot(options) {
return result;
}


function runDiffImageToSnapshot(options) {
options.receivedImageBuffer = options.receivedImageBuffer.toString('base64');

Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function configureToMatchImageSnapshot({
storeReceivedOnFailure: commonStoreReceivedOnFailure = false,
customReceivedDir: commonCustomReceivedDir,
customDiffDir: commonCustomDiffDir,
onlyDiff: commonOnlyDiff = false,
diffDirection: commonDiffDirection = 'horizontal',
noColors: commonNoColors,
failureThreshold: commonFailureThreshold = 0,
Expand All @@ -156,6 +157,7 @@ function configureToMatchImageSnapshot({
storeReceivedOnFailure = commonStoreReceivedOnFailure,
customReceivedDir = commonCustomReceivedDir,
customDiffDir = commonCustomDiffDir,
onlyDiff = commonOnlyDiff,
diffDirection = commonDiffDirection,
customDiffConfig = {},
noColors = commonNoColors,
Expand Down Expand Up @@ -218,6 +220,7 @@ function configureToMatchImageSnapshot({
receivedDir,
diffDir,
diffDirection,
onlyDiff,
snapshotIdentifier,
updateSnapshot: snapshotState._updateSnapshot === 'all',
customDiffConfig: Object.assign({}, commonCustomDiffConfig, customDiffConfig),
Expand Down