Skip to content

Commit d00eb27

Browse files
WestbrookLarsDenBakker
authored andcommitted
fix(test-runner-visual-regression): allow mismatched screenshot sizes to be diffed
1 parent f2733bb commit d00eb27

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

packages/test-runner-visual-regression/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type OptionalImage = Buffer | undefined | Promise<Buffer | undefined>;
2828
export interface DiffResult {
2929
diffPercentage: number;
3030
diffImage: Buffer;
31+
error: string;
3132
}
3233

3334
export interface DiffArgs {

packages/test-runner-visual-regression/src/pixelMatchDiff.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
import pixelmatch from 'pixelmatch';
2-
import { PNG } from 'pngjs';
2+
import { PNG, PNGWithMetadata } from 'pngjs';
33

44
import { DiffArgs, DiffResult } from './config';
5-
import { VisualRegressionError } from './VisualRegressionError';
65

76
export function pixelMatchDiff({ baselineImage, image, options }: DiffArgs): DiffResult {
8-
const basePng = PNG.sync.read(baselineImage);
9-
const png = PNG.sync.read(image);
7+
let error = '';
8+
let basePng: PNG | PNGWithMetadata = PNG.sync.read(baselineImage);
9+
let png: PNG | PNGWithMetadata = PNG.sync.read(image);
10+
let { width, height } = png;
1011

1112
if (basePng.width !== png.width || basePng.height !== png.height) {
12-
throw new VisualRegressionError(
13+
error =
1314
`Screenshot is not the same width and height as the baseline. ` +
14-
`Baseline: { width: ${basePng.width}, height: ${basePng.height} }` +
15-
`Screenshot: { width: ${png.width}, height: ${png.height} }`,
16-
);
15+
`Baseline: { width: ${basePng.width}, height: ${basePng.height} }` +
16+
`Screenshot: { width: ${png.width}, height: ${png.height} }`;
17+
width = Math.max(basePng.width, png.width);
18+
height = Math.max(basePng.height, png.height);
19+
let oldPng = basePng;
20+
basePng = new PNG({ width, height });
21+
oldPng.data.copy(basePng.data, 0, 0, oldPng.data.length);
22+
oldPng = png;
23+
png = new PNG({ width, height });
24+
oldPng.data.copy(png.data, 0, 0, oldPng.data.length);
1725
}
1826

19-
const width = png.width;
20-
const height = png.height;
21-
2227
const diff = new PNG({ width, height });
2328

2429
const numDiffPixels = pixelmatch(basePng.data, png.data, diff.data, width, height, options);
2530
const diffPercentage = (numDiffPixels / (width * height)) * 100;
2631

2732
return {
33+
error,
2834
diffImage: PNG.sync.write(diff),
2935
diffPercentage,
3036
};

packages/test-runner-visual-regression/src/visualDiffCommand.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22

33
import { VisualRegressionPluginOptions } from './config';
4+
import { VisualRegressionError } from './VisualRegressionError';
45

56
function resolveImagePath(baseDir: string, name: string) {
67
const finalName = path.extname(name) ? name : `${name}.png`;
@@ -53,6 +54,15 @@ export async function visualDiffCommand(
5354
});
5455
};
5556

57+
const saveDiff = async () => {
58+
await options.saveDiff({
59+
filePath: resolveImagePath(baseDir, diffName),
60+
baseDir,
61+
name: diffName,
62+
content: diffImage,
63+
});
64+
};
65+
5666
if (!baselineImage) {
5767
await saveFailed();
5868

@@ -63,21 +73,25 @@ export async function visualDiffCommand(
6373
};
6474
}
6575

66-
const { diffImage, diffPercentage } = await options.getImageDiff({
76+
const { diffImage, diffPercentage, error } = await options.getImageDiff({
6777
name,
6878
baselineImage,
6979
image,
7080
options: options.diffOptions,
7181
});
82+
83+
if (error) {
84+
// The diff has failed, be sure to save the new image.
85+
await saveFailed();
86+
await saveDiff();
87+
88+
throw new VisualRegressionError(error);
89+
}
90+
7291
const passed = diffPercentage === 0;
7392

7493
if (!passed) {
75-
await options.saveDiff({
76-
filePath: resolveImagePath(baseDir, diffName),
77-
baseDir,
78-
name: diffName,
79-
content: diffImage,
80-
});
94+
await saveDiff();
8195
}
8296

8397
if (!passed || options.buildCache) {

0 commit comments

Comments
 (0)