Skip to content

Commit 1be1b00

Browse files
perf(diff-snapshot): remove logic to bypass diff for identical images
pixelmatch now handles this for us
1 parent 79d59c9 commit 1be1b00

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

__tests__/diff-snapshot.spec.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ describe('diff-snapshot', () => {
138138
diffPixelCount: 0,
139139
pass: true,
140140
});
141-
// Check that pixelmatch was not called
142-
expect(mockPixelMatch).not.toHaveBeenCalled();
143141
});
144142

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

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

350346
// Check that pixelmatch was not called
351-
expect(mockPixelMatch).not.toHaveBeenCalled();
347+
expect(mockPixelMatch).toHaveBeenCalledWith(
348+
expect.any(Object), // buffer data
349+
expect.any(Object), // buffer data
350+
expect.any(Object), // buffer data
351+
expect.any(Number), // image width
352+
expect.any(Number), // image height
353+
{ threshold: 0.01 }
354+
);
352355
});
353356

354357
it('should merge custom configuration with default configuration if custom config is passed', () => {
@@ -361,15 +364,21 @@ describe('diff-snapshot', () => {
361364
diffDir: mockDiffDir,
362365
updateSnapshot: false,
363366
customDiffConfig: {
364-
threshold: 0.1,
365367
foo: 'bar',
366368
},
367369
failureThreshold: 0,
368370
failureThresholdType: 'pixel',
369371
});
370372

371373
// Check that pixelmatch was not called
372-
expect(mockPixelMatch).not.toHaveBeenCalled();
374+
expect(mockPixelMatch).toHaveBeenCalledWith(
375+
expect.any(Object), // buffer data
376+
expect.any(Object), // buffer data
377+
expect.any(Object), // buffer data
378+
expect.any(Number), // image width
379+
expect.any(Number), // image height
380+
{ foo: 'bar', threshold: 0.01 }
381+
);
373382
});
374383

375384
it('should create diff output directory if there is not one already and test is failing', () => {

src/diff-snapshot.js

+20-29
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const mkdirp = require('mkdirp');
1919
const pixelmatch = require('pixelmatch');
2020
const { PNG } = require('pngjs');
2121
const rimraf = require('rimraf');
22-
const { createHash } = require('crypto');
2322
const glur = require('glur');
2423
const ImageComposer = require('./image-composer');
2524

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

8786
function diffImageToSnapshot(options) {
88-
/* eslint complexity: ["error", 12] */
8987
const {
9088
receivedImageBuffer,
9189
snapshotIdentifier,
@@ -147,34 +145,27 @@ function diffImageToSnapshot(options) {
147145
let diffRatio = 0;
148146
let diffPixelCount = 0;
149147

150-
const receivedImageDigest = createHash('sha1').update(receivedImage.data).digest('base64');
151-
const baselineImageDigest = createHash('sha1').update(baselineImage.data).digest('base64');
152-
153-
pass = receivedImageDigest === baselineImageDigest;
154-
155-
if (!pass) {
156-
diffPixelCount = pixelmatch(
157-
receivedImage.data,
158-
baselineImage.data,
159-
diffImage.data,
160-
imageWidth,
161-
imageHeight,
162-
diffConfig
163-
);
148+
diffPixelCount = pixelmatch(
149+
receivedImage.data,
150+
baselineImage.data,
151+
diffImage.data,
152+
imageWidth,
153+
imageHeight,
154+
diffConfig
155+
);
164156

165-
const totalPixels = imageWidth * imageHeight;
166-
diffRatio = diffPixelCount / totalPixels;
167-
// Always fail test on image size mismatch
168-
if (hasSizeMismatch) {
169-
pass = false;
170-
diffSize = true;
171-
} else if (failureThresholdType === 'pixel') {
172-
pass = diffPixelCount <= failureThreshold;
173-
} else if (failureThresholdType === 'percent') {
174-
pass = diffRatio <= failureThreshold;
175-
} else {
176-
throw new Error(`Unknown failureThresholdType: ${failureThresholdType}. Valid options are "pixel" or "percent".`);
177-
}
157+
const totalPixels = imageWidth * imageHeight;
158+
diffRatio = diffPixelCount / totalPixels;
159+
// Always fail test on image size mismatch
160+
if (hasSizeMismatch) {
161+
pass = false;
162+
diffSize = true;
163+
} else if (failureThresholdType === 'pixel') {
164+
pass = diffPixelCount <= failureThreshold;
165+
} else if (failureThresholdType === 'percent') {
166+
pass = diffRatio <= failureThreshold;
167+
} else {
168+
throw new Error(`Unknown failureThresholdType: ${failureThresholdType}. Valid options are "pixel" or "percent".`);
178169
}
179170

180171
if (isFailure({ pass, updateSnapshot })) {

0 commit comments

Comments
 (0)