Skip to content

Commit

Permalink
feat: add onlyDiff in options (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayc0 committed Dec 2, 2022
1 parent 8a37453 commit 4bad752
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
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
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
39 changes: 39 additions & 0 deletions __tests__/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,45 @@ describe('toMatchImageSnapshot', () => {
.toBe(false);
});

it('only outputs the diff when onlyDiff is enabled', () => {
const failureImageData = fs.readFileSync(fromStubs('TestImageUpdate1pxOff.png'));
const imageFailureOnlyDiffData =
fs.readFileSync(fromStubs('TestImageUpdate1pxOff-onlyDiff-diff.png'));

const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
const pathToResultImage = path.join(__dirname, diffOutputDir(), `${customSnapshotIdentifier}-diff.png`);
// First we need to write a new snapshot image
expect(
() => expect(imageData)
.toMatchImageSnapshot({
customSnapshotIdentifier,
onlyDiff: true,
})
)
.not
.toThrowError();

// then test against a different image
expect(
() => expect(failureImageData)
.toMatchImageSnapshot({
customSnapshotIdentifier,
onlyDiff: true,
// required for coverage
runInProcess: true,
})
)
.toThrow(/Expected image to match or be a close match/);

expect(fs.existsSync(pathToResultImage))
.toBe(true);

expect(fs.readFileSync(pathToResultImage)).toEqual(imageFailureOnlyDiffData);
// just because file was written does not mean it is a png image
expect(sizeOf(pathToResultImage))
.toHaveProperty('type', 'png');
});

it('handles diffs for large images', () => {
const largeImageData = fs.readFileSync(fromStubs('LargeTestImage.png'));
const largeFailureImageData = fs.readFileSync(fromStubs('LargeTestImageFailure.png'));
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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(diffImage, imageWidth, imageHeight);
} else {
composer.addImage(baselineImage, imageWidth, imageHeight);
composer.addImage(diffImage, imageWidth, imageHeight);
composer.addImage(receivedImage, imageWidth, imageHeight);
}
return composer;
}

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

0 comments on commit 4bad752

Please sign in to comment.