Skip to content

Commit

Permalink
perf(diff-snapshot): remove logic to bypass diff for identical images
Browse files Browse the repository at this point in the history
pixelmatch now handles this for us
  • Loading branch information
anescobar1991 committed Mar 25, 2020
1 parent 79d59c9 commit 1be1b00
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
23 changes: 16 additions & 7 deletions __tests__/diff-snapshot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ describe('diff-snapshot', () => {
diffPixelCount: 0,
pass: true,
});
// Check that pixelmatch was not called
expect(mockPixelMatch).not.toHaveBeenCalled();
});

it('it should not write a diff if a test passes', () => {
Expand All @@ -160,8 +158,6 @@ describe('diff-snapshot', () => {
diffPixelCount: 0,
pass: true,
});
// Check that pixelmatch was not called
expect(mockPixelMatch).not.toHaveBeenCalled();

// Check that that it did not attempt to write a diff
expect(mockWriteFileSync.mock.calls).toEqual([]);
Expand Down Expand Up @@ -348,7 +344,14 @@ describe('diff-snapshot', () => {
});

// Check that pixelmatch was not called
expect(mockPixelMatch).not.toHaveBeenCalled();
expect(mockPixelMatch).toHaveBeenCalledWith(
expect.any(Object), // buffer data
expect.any(Object), // buffer data
expect.any(Object), // buffer data
expect.any(Number), // image width
expect.any(Number), // image height
{ threshold: 0.01 }
);
});

it('should merge custom configuration with default configuration if custom config is passed', () => {
Expand All @@ -361,15 +364,21 @@ describe('diff-snapshot', () => {
diffDir: mockDiffDir,
updateSnapshot: false,
customDiffConfig: {
threshold: 0.1,
foo: 'bar',
},
failureThreshold: 0,
failureThresholdType: 'pixel',
});

// Check that pixelmatch was not called
expect(mockPixelMatch).not.toHaveBeenCalled();
expect(mockPixelMatch).toHaveBeenCalledWith(
expect.any(Object), // buffer data
expect.any(Object), // buffer data
expect.any(Object), // buffer data
expect.any(Number), // image width
expect.any(Number), // image height
{ foo: 'bar', threshold: 0.01 }
);
});

it('should create diff output directory if there is not one already and test is failing', () => {
Expand Down
49 changes: 20 additions & 29 deletions src/diff-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const mkdirp = require('mkdirp');
const pixelmatch = require('pixelmatch');
const { PNG } = require('pngjs');
const rimraf = require('rimraf');
const { createHash } = require('crypto');
const glur = require('glur');
const ImageComposer = require('./image-composer');

Expand Down Expand Up @@ -85,7 +84,6 @@ const shouldUpdate = ({ pass, updateSnapshot, updatePassedSnapshot }) => (
);

function diffImageToSnapshot(options) {
/* eslint complexity: ["error", 12] */
const {
receivedImageBuffer,
snapshotIdentifier,
Expand Down Expand Up @@ -147,34 +145,27 @@ function diffImageToSnapshot(options) {
let diffRatio = 0;
let diffPixelCount = 0;

const receivedImageDigest = createHash('sha1').update(receivedImage.data).digest('base64');
const baselineImageDigest = createHash('sha1').update(baselineImage.data).digest('base64');

pass = receivedImageDigest === baselineImageDigest;

if (!pass) {
diffPixelCount = pixelmatch(
receivedImage.data,
baselineImage.data,
diffImage.data,
imageWidth,
imageHeight,
diffConfig
);
diffPixelCount = pixelmatch(
receivedImage.data,
baselineImage.data,
diffImage.data,
imageWidth,
imageHeight,
diffConfig
);

const totalPixels = imageWidth * imageHeight;
diffRatio = diffPixelCount / totalPixels;
// Always fail test on image size mismatch
if (hasSizeMismatch) {
pass = false;
diffSize = true;
} else if (failureThresholdType === 'pixel') {
pass = diffPixelCount <= failureThreshold;
} else if (failureThresholdType === 'percent') {
pass = diffRatio <= failureThreshold;
} else {
throw new Error(`Unknown failureThresholdType: ${failureThresholdType}. Valid options are "pixel" or "percent".`);
}
const totalPixels = imageWidth * imageHeight;
diffRatio = diffPixelCount / totalPixels;
// Always fail test on image size mismatch
if (hasSizeMismatch) {
pass = false;
diffSize = true;
} else if (failureThresholdType === 'pixel') {
pass = diffPixelCount <= failureThreshold;
} else if (failureThresholdType === 'percent') {
pass = diffRatio <= failureThreshold;
} else {
throw new Error(`Unknown failureThresholdType: ${failureThresholdType}. Valid options are "pixel" or "percent".`);
}

if (isFailure({ pass, updateSnapshot })) {
Expand Down

0 comments on commit 1be1b00

Please sign in to comment.