From f1681e390684f695101113ed748021d88b121aa5 Mon Sep 17 00:00:00 2001 From: Manos Konstantinidis Date: Fri, 8 Oct 2021 14:51:44 +0100 Subject: [PATCH] Skip generating a diff image if there are no differences MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With a single test case: **Before** ✨ Done in 12.45s. **After** ✨ Done in 4.34s. --- src/matchers.test.ts | 4 ++-- src/matchers.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/matchers.test.ts b/src/matchers.test.ts index ce53b278..e7704f3d 100644 --- a/src/matchers.test.ts +++ b/src/matchers.test.ts @@ -47,10 +47,10 @@ describe('matchers.ts', () => { const result = toMatchBaseline(latestPath); expect(result.message()).toBe( - 'Compared screenshot to match baseline. 0 were different.' + 'Compared screenshot to match baseline. No differences were found.' ); expect(result.pass).toBe(true); - expect(writeFileMock).toHaveBeenCalledTimes(1); + expect(writeFileMock).toHaveBeenCalledTimes(0); }); it('should compare two different images', () => { diff --git a/src/matchers.ts b/src/matchers.ts index 30e44a59..7ee5087f 100644 --- a/src/matchers.ts +++ b/src/matchers.ts @@ -50,6 +50,14 @@ export const toMatchBaseline = (latestPath: string) => { colorThreshold: 0.1, }); + if (pixels === 0) { + return { + message: () => + `Compared screenshot to match baseline. No differences were found.`, + pass: true, + }; + } + fs.mkdirSync(path.dirname(diffPath), { recursive: true }); const diffPng = { ...diff! } as PNG;