Skip to content

Commit 5da0aac

Browse files
committed
feat: img diff when resolution differs
BREAKING CHANGE: different resolution doesn't fail test immediately - img diff is being done closes #94 Signed-off-by: Jakub Freisler <[email protected]>
1 parent ac3b059 commit 5da0aac

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/plugins.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import sanitize from "sanitize-filename";
99

1010
type NotFalsy<T> = T extends false | null | undefined ? never : T;
1111

12+
const round = (n: number) => Math.ceil(n * 1000) / 1000;
13+
1214
const createImageResizer = (width: number, height: number) => (source: PNG) => {
1315
const resized = new PNG({ width, height, fill: true });
1416
PNG.bitblt(source, resized, 0, 0, source.width, source.height, 0, 0);
@@ -131,9 +133,17 @@ export const initPlugin = (
131133
maxDiffThreshold: number;
132134
diffConfig: Parameters<typeof pixelmatch>[5];
133135
} & Parameters<typeof pixelmatch>[5]
134-
) {
136+
): Promise<
137+
null | {
138+
error?: boolean;
139+
message?: string;
140+
imgDiff?: number;
141+
maxDiffThreshold?: number;
142+
}
143+
> {
144+
const messages = [] as string[];
135145
let imgDiff: number | undefined;
136-
let errorMsg: string | undefined;
146+
let error = false;
137147

138148
if (fs.existsSync(cfg.imgOld) && !cfg.updateImages) {
139149
const rawImgNew = await importAndScaleImage({
@@ -164,20 +174,22 @@ export const initPlugin = (
164174
imgDiff = diffPixels / (width * height);
165175

166176
if (isImgSizeDifferent) {
167-
errorMsg = `Images size mismatch - new screenshot is ${rawImgNew.width}px by ${rawImgNew.height}px while old one is ${rawImgOld.width}px by ${rawImgOld.height} (width x height).`;
168-
} else if (imgDiff > cfg.maxDiffThreshold) {
169-
const roundedImgDiff = Math.ceil(imgDiff * 1000) / 1000;
170-
errorMsg = `Image diff factor (${roundedImgDiff}) is bigger than maximum threshold option ${cfg.maxDiffThreshold}`;
177+
messages.push(`Warning: Images size mismatch - new screenshot is ${rawImgNew.width}px by ${rawImgNew.height}px while old one is ${rawImgOld.width}px by ${rawImgOld.height} (width x height).`);
178+
}
179+
180+
if (imgDiff > cfg.maxDiffThreshold) {
181+
messages.unshift(`Image diff factor (${round(imgDiff)}) is bigger than maximum threshold option ${cfg.maxDiffThreshold}.`);
182+
error = true;
171183
}
172184

173-
if (errorMsg) {
185+
if (error) {
174186
fs.writeFileSync(
175187
cfg.imgNew.replace(FILE_SUFFIX.actual, FILE_SUFFIX.diff),
176188
PNG.sync.write(diff)
177189
);
178190
return {
179-
error: true,
180-
message: errorMsg,
191+
error,
192+
message: messages.join('\n'),
181193
imgDiff,
182194
maxDiffThreshold: cfg.maxDiffThreshold,
183195
};
@@ -192,9 +204,9 @@ export const initPlugin = (
192204
}
193205

194206
if (typeof imgDiff !== "undefined") {
195-
const roundedImgDiff = Math.ceil(imgDiff * 1000) / 1000;
207+
messages.unshift(`Image diff (${round(imgDiff)}%) is within boundaries of maximum threshold option ${cfg.maxDiffThreshold}.`);
196208
return {
197-
message: `Image diff (${roundedImgDiff}%) is within boundaries of maximum threshold option ${cfg.maxDiffThreshold}`,
209+
message: messages.join('\n'),
198210
imgDiff,
199211
maxDiffThreshold: cfg.maxDiffThreshold,
200212
};

0 commit comments

Comments
 (0)