Skip to content

Commit

Permalink
test: improve image-diff resize (#46986)
Browse files Browse the repository at this point in the history
* ci: enable splitTheme

* chore: remove blue border

* test: improve image-diff when size is different
  • Loading branch information
vagusX committed Jan 15, 2024
1 parent 9490179 commit 46abe90
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions scripts/visual-regression/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,38 @@ const compareScreenshots = async (
const currentImgBuf = await sharp(currentImgPath).toBuffer();

const basePng = PNG.sync.read(baseImgBuf);
const targetWidth = basePng.width;
const targetHeight = basePng.height;

const comparePng = PNG.sync.read(
await sharp(currentImgBuf)
.resize({
width: targetWidth,
height: targetHeight,
fit: sharp.fit.contain,
background: { r: 255, g: 255, b: 255, alpha: 0 },
})
.png()
.toBuffer(),
const currentPng = PNG.sync.read(currentImgBuf);

const targetWidth = Math.max(basePng.width, currentPng.width);
const targetHeight = Math.max(basePng.height, currentPng.height);

// fill color for transparent png
const fillColor =
baseImgPath.endsWith('dark.png') || baseImgPath.endsWith('dark.css-var.png')
? { r: 0, g: 0, b: 0, alpha: 255 }
: { r: 255, g: 255, b: 255, alpha: 255 };

const resizeOptions = {
width: targetWidth,
height: targetHeight,
position: 'left top',
fit: sharp.fit.contain,
background: fillColor,
};

const resizedBasePng = PNG.sync.read(
await sharp(baseImgBuf).resize(resizeOptions).png().toBuffer(),
);

const resizedCurrentPng = PNG.sync.read(
await sharp(currentImgBuf).resize(resizeOptions).png().toBuffer(),
);

const diffPng = new PNG({ width: targetWidth, height: targetHeight });

const mismatchedPixels = pixelmatch(
basePng.data,
comparePng.data,
resizedBasePng.data,
resizedCurrentPng.data,
diffPng.data,
targetWidth,
targetHeight,
Expand Down

0 comments on commit 46abe90

Please sign in to comment.