Skip to content

fix(test-runner-visual-regression): allow mismatched screenshot sizes to be diffed #1352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/few-sloths-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@web/test-runner-visual-regression": patch
"@web/test-runner": patch
---

Capture visual regressions across changing screenshot sizes.
1 change: 1 addition & 0 deletions packages/test-runner-visual-regression/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type OptionalImage = Buffer | undefined | Promise<Buffer | undefined>;
export interface DiffResult {
diffPercentage: number;
diffImage: Buffer;
error: string;
}

export interface DiffArgs {
Expand Down
28 changes: 17 additions & 11 deletions packages/test-runner-visual-regression/src/pixelMatchDiff.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
import { PNG, PNGWithMetadata } from 'pngjs';

import { DiffArgs, DiffResult } from './config';
import { VisualRegressionError } from './VisualRegressionError';

export function pixelMatchDiff({ baselineImage, image, options }: DiffArgs): DiffResult {
const basePng = PNG.sync.read(baselineImage);
const png = PNG.sync.read(image);
let error = '';
let basePng: PNG | PNGWithMetadata = PNG.sync.read(baselineImage);
let png: PNG | PNGWithMetadata = PNG.sync.read(image);
let { width, height } = png;

if (basePng.width !== png.width || basePng.height !== png.height) {
throw new VisualRegressionError(
error =
`Screenshot is not the same width and height as the baseline. ` +
`Baseline: { width: ${basePng.width}, height: ${basePng.height} }` +
`Screenshot: { width: ${png.width}, height: ${png.height} }`,
);
`Baseline: { width: ${basePng.width}, height: ${basePng.height} }` +
`Screenshot: { width: ${png.width}, height: ${png.height} }`;
width = Math.max(basePng.width, png.width);
height = Math.max(basePng.height, png.height);
let oldPng = basePng;
basePng = new PNG({ width, height });
oldPng.data.copy(basePng.data, 0, 0, oldPng.data.length);
oldPng = png;
png = new PNG({ width, height });
oldPng.data.copy(png.data, 0, 0, oldPng.data.length);
}

const width = png.width;
const height = png.height;

const diff = new PNG({ width, height });

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

return {
error,
diffImage: PNG.sync.write(diff),
diffPercentage,
};
Expand Down
28 changes: 21 additions & 7 deletions packages/test-runner-visual-regression/src/visualDiffCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';

import { VisualRegressionPluginOptions } from './config';
import { VisualRegressionError } from './VisualRegressionError';

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

const saveDiff = async () => {
await options.saveDiff({
filePath: resolveImagePath(baseDir, diffName),
baseDir,
name: diffName,
content: diffImage,
});
};

if (!baselineImage) {
await saveFailed();

Expand All @@ -63,21 +73,25 @@ export async function visualDiffCommand(
};
}

const { diffImage, diffPercentage } = await options.getImageDiff({
const { diffImage, diffPercentage, error } = await options.getImageDiff({
name,
baselineImage,
image,
options: options.diffOptions,
});

if (error) {
// The diff has failed, be sure to save the new image.
await saveFailed();
await saveDiff();

throw new VisualRegressionError(error);
}

const passed = diffPercentage === 0;

if (!passed) {
await options.saveDiff({
filePath: resolveImagePath(baseDir, diffName),
baseDir,
name: diffName,
content: diffImage,
});
await saveDiff();
}

if (!passed || options.buildCache) {
Expand Down