Skip to content

Commit c8a5044

Browse files
authored
feat: img diff when resolution differs (#108)
BREAKING CHANGE: different resolution doesn't fail test immediately - img diff is being done closes #94
1 parent ac3b059 commit c8a5044

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

src/plugins.ts

Lines changed: 35 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,15 @@ export const initPlugin = (
131133
maxDiffThreshold: number;
132134
diffConfig: Parameters<typeof pixelmatch>[5];
133135
} & Parameters<typeof pixelmatch>[5]
134-
) {
136+
): Promise<null | {
137+
error?: boolean;
138+
message?: string;
139+
imgDiff?: number;
140+
maxDiffThreshold?: number;
141+
}> {
142+
const messages = [] as string[];
135143
let imgDiff: number | undefined;
136-
let errorMsg: string | undefined;
144+
let error = false;
137145

138146
if (fs.existsSync(cfg.imgOld) && !cfg.updateImages) {
139147
const rawImgNew = await importAndScaleImage({
@@ -164,20 +172,30 @@ export const initPlugin = (
164172
imgDiff = diffPixels / (width * height);
165173

166174
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}`;
175+
messages.push(
176+
`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).`
177+
);
178+
}
179+
180+
if (imgDiff > cfg.maxDiffThreshold) {
181+
messages.unshift(
182+
`Image diff factor (${round(
183+
imgDiff
184+
)}) is bigger than maximum threshold option ${
185+
cfg.maxDiffThreshold
186+
}.`
187+
);
188+
error = true;
171189
}
172190

173-
if (errorMsg) {
191+
if (error) {
174192
fs.writeFileSync(
175193
cfg.imgNew.replace(FILE_SUFFIX.actual, FILE_SUFFIX.diff),
176194
PNG.sync.write(diff)
177195
);
178196
return {
179-
error: true,
180-
message: errorMsg,
197+
error,
198+
message: messages.join("\n"),
181199
imgDiff,
182200
maxDiffThreshold: cfg.maxDiffThreshold,
183201
};
@@ -192,9 +210,15 @@ export const initPlugin = (
192210
}
193211

194212
if (typeof imgDiff !== "undefined") {
195-
const roundedImgDiff = Math.ceil(imgDiff * 1000) / 1000;
213+
messages.unshift(
214+
`Image diff (${round(
215+
imgDiff
216+
)}%) is within boundaries of maximum threshold option ${
217+
cfg.maxDiffThreshold
218+
}.`
219+
);
196220
return {
197-
message: `Image diff (${roundedImgDiff}%) is within boundaries of maximum threshold option ${cfg.maxDiffThreshold}`,
221+
message: messages.join("\n"),
198222
imgDiff,
199223
maxDiffThreshold: cfg.maxDiffThreshold,
200224
};

0 commit comments

Comments
 (0)