From 1be1b006220b4144f98ad583c8cd6ff629aec7b3 Mon Sep 17 00:00:00 2001 From: Andres Escobar Date: Wed, 25 Mar 2020 10:56:16 -0700 Subject: [PATCH] perf(diff-snapshot): remove logic to bypass diff for identical images pixelmatch now handles this for us --- __tests__/diff-snapshot.spec.js | 23 +++++++++++----- src/diff-snapshot.js | 49 ++++++++++++++------------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/__tests__/diff-snapshot.spec.js b/__tests__/diff-snapshot.spec.js index d3e1df7..7270668 100644 --- a/__tests__/diff-snapshot.spec.js +++ b/__tests__/diff-snapshot.spec.js @@ -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', () => { @@ -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([]); @@ -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', () => { @@ -361,7 +364,6 @@ describe('diff-snapshot', () => { diffDir: mockDiffDir, updateSnapshot: false, customDiffConfig: { - threshold: 0.1, foo: 'bar', }, failureThreshold: 0, @@ -369,7 +371,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 + { foo: 'bar', threshold: 0.01 } + ); }); it('should create diff output directory if there is not one already and test is failing', () => { diff --git a/src/diff-snapshot.js b/src/diff-snapshot.js index 956c9cf..245eaf9 100644 --- a/src/diff-snapshot.js +++ b/src/diff-snapshot.js @@ -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'); @@ -85,7 +84,6 @@ const shouldUpdate = ({ pass, updateSnapshot, updatePassedSnapshot }) => ( ); function diffImageToSnapshot(options) { - /* eslint complexity: ["error", 12] */ const { receivedImageBuffer, snapshotIdentifier, @@ -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 })) {