Skip to content

Commit 585eca9

Browse files
ArtmannNimaSoroush
authored andcommitted
Change the distance comparison 📐 (#140)
Before this change, if you wanted to do pixel perfect comparison your only option was to specify a very small theshold and even doing that did not always yield the expected result. Changing the image comparison (distance & percetage) from less-than to less-than-or-equal-to will allow a treshold of zero.
1 parent b429b2b commit 585eca9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/compareImage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const compareImage = async (capturedImage, globalConfig, testConfig) => {
6868
prefixedLogger.log('comparing...');
6969
const distance = Jimp.distance(snapshotImage, testImage);
7070
const diff = Jimp.diff(snapshotImage, testImage, globalConfig.mismatchThreshold);
71-
if (distance < globalConfig.mismatchThreshold && diff.percent < globalConfig.mismatchThreshold) {
71+
if (distance <= globalConfig.mismatchThreshold && diff.percent <= globalConfig.mismatchThreshold) {
7272
prefixedLogger.log('no mismatch found ✅');
7373
return {
7474
snapshotPath, distance, diffPercent: diff.percent, matched: true,

src/compareImage.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ describe('Compare Image', () => {
158158
expect(mockLog).toHaveBeenCalledWith('no mismatch found ✅');
159159
});
160160

161+
it('returns correct value if difference below threshold when the threshold is set to zero', async () => {
162+
expect.assertions(2);
163+
164+
fs.existsSync.mockReturnValueOnce(true);
165+
166+
const config = {
167+
...mockConfig,
168+
mismatchThreshold: 0,
169+
};
170+
const result = await compareImage(Object, config, mockTestConfig);
171+
172+
expect(result).toEqual({
173+
diffPercent: 0,
174+
distance: 0,
175+
matched: true,
176+
snapshotPath: '/parent/__image_snapshots__/test.snap.png',
177+
});
178+
179+
expect(mockLog).toHaveBeenCalledWith('no mismatch found ✅');
180+
});
181+
161182
it('returns mismatch found❗ if only difference above threshold', async () => {
162183
Jimp.diff.mockReturnValue({
163184
percent: 0.02,

0 commit comments

Comments
 (0)