Skip to content

Commit 252995c

Browse files
authored
Do not compare image if the same buffer (#126)
closes: Visual-Regression-Tracker/Visual-Regression-Tracker#248
1 parent ec4cd90 commit 252995c

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

src/test-runs/test-runs.service.spec.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,28 @@ describe('TestRunsService', () => {
315315
});
316316
});
317317

318+
it('diff not found', async () => {
319+
const baseline = new PNG({
320+
width: 20,
321+
height: 20,
322+
});
323+
const image = new PNG({
324+
width: 20,
325+
height: 20,
326+
});
327+
service = await initService({});
328+
329+
const result = service.getDiff(baseline, image, baseTestRun);
330+
331+
expect(result).toStrictEqual({
332+
status: TestStatus.ok,
333+
diffName: null,
334+
pixelMisMatchCount: 0,
335+
diffPercent: 0,
336+
isSameDimension: true,
337+
});
338+
});
339+
318340
it('diff image dimensions mismatch', async () => {
319341
delete process.env.ALLOW_DIFF_DIMENSIONS;
320342
const baseline = new PNG({
@@ -341,35 +363,35 @@ describe('TestRunsService', () => {
341363
it('diff image dimensions mismatch ALLOWED', async () => {
342364
process.env.ALLOW_DIFF_DIMENSIONS = 'true';
343365
const baseline = new PNG({
344-
width: 20,
345-
height: 10,
366+
width: 1,
367+
height: 5,
346368
});
347369
const image = new PNG({
348-
width: 10,
349-
height: 20,
370+
width: 2,
371+
height: 4,
350372
});
351373
const diffName = 'diff name';
352374
const saveImageMock = jest.fn().mockReturnValueOnce(diffName);
353-
mocked(Pixelmatch).mockReturnValueOnce(200);
375+
mocked(Pixelmatch).mockReturnValueOnce(5);
354376
service = await initService({ saveImageMock });
355377

356378
const result = service.getDiff(baseline, image, baseTestRun);
357379

358380
expect(mocked(Pixelmatch)).toHaveBeenCalledWith(
359381
new PNG({
360-
width: 20,
361-
height: 20,
382+
width: 2,
383+
height: 5,
362384
}).data,
363385
new PNG({
364-
width: 20,
365-
height: 20,
386+
width: 2,
387+
height: 5,
366388
}).data,
367389
new PNG({
368-
width: 20,
369-
height: 20,
390+
width: 2,
391+
height: 5,
370392
}).data,
371-
20,
372-
20,
393+
2,
394+
5,
373395
{
374396
includeAA: true,
375397
}
@@ -378,35 +400,12 @@ describe('TestRunsService', () => {
378400
expect(result).toStrictEqual({
379401
status: TestStatus.unresolved,
380402
diffName,
381-
pixelMisMatchCount: 200,
403+
pixelMisMatchCount: 5,
382404
diffPercent: 50,
383405
isSameDimension: false,
384406
});
385407
});
386408

387-
it('diff not found', async () => {
388-
const baseline = new PNG({
389-
width: 10,
390-
height: 10,
391-
});
392-
const image = new PNG({
393-
width: 10,
394-
height: 10,
395-
});
396-
service = await initService({});
397-
mocked(Pixelmatch).mockReturnValueOnce(0);
398-
399-
const result = service.getDiff(baseline, image, baseTestRun);
400-
401-
expect(result).toStrictEqual({
402-
status: TestStatus.ok,
403-
diffName: null,
404-
pixelMisMatchCount: 0,
405-
diffPercent: 0,
406-
isSameDimension: true,
407-
});
408-
});
409-
410409
it('diff found < tollerance', async () => {
411410
const testRun: TestRun = {
412411
...baseTestRun,
@@ -418,6 +417,7 @@ describe('TestRunsService', () => {
418417
width: 100,
419418
height: 100,
420419
});
420+
baseline.data[0] = 1
421421
const image = new PNG({
422422
width: 100,
423423
height: 100,
@@ -450,6 +450,7 @@ describe('TestRunsService', () => {
450450
width: 100,
451451
height: 100,
452452
});
453+
baseline.data[0] = 1
453454
const image = new PNG({
454455
width: 100,
455456
height: 100,

src/test-runs/test-runs.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,20 @@ export class TestRunsService {
270270

271271
result.isSameDimension = baseline.width === image.width && baseline.height === image.height;
272272

273+
if (baseline.data.equals(image.data)) {
274+
// equal images
275+
result.status = TestStatus.ok;
276+
result.pixelMisMatchCount = 0;
277+
result.diffPercent = 0;
278+
return result;
279+
}
280+
273281
if (!result.isSameDimension && !process.env.ALLOW_DIFF_DIMENSIONS) {
274282
// diff dimensions
275283
result.status = TestStatus.unresolved;
276284
return result;
277285
}
286+
278287
// scale image to max size
279288
const maxWidth = Math.max(baseline.width, image.width);
280289
const maxHeight = Math.max(baseline.height, image.height);

0 commit comments

Comments
 (0)