@web/test-runner-visual-regression
Version:
Web test runner visual regression
39 lines • 1.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pixelMatchDiff = void 0;
const pixelmatch_1 = __importDefault(require("pixelmatch"));
const pngjs_1 = require("pngjs");
function pixelMatchDiff({ baselineImage, image, options }) {
let error = '';
let basePng = pngjs_1.PNG.sync.read(baselineImage);
let png = pngjs_1.PNG.sync.read(image);
let { width, height } = png;
if (basePng.width !== png.width || basePng.height !== png.height) {
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} }`;
width = Math.max(basePng.width, png.width);
height = Math.max(basePng.height, png.height);
let oldPng = basePng;
basePng = new pngjs_1.PNG({ width, height });
oldPng.data.copy(basePng.data, 0, 0, oldPng.data.length);
oldPng = png;
png = new pngjs_1.PNG({ width, height });
oldPng.data.copy(png.data, 0, 0, oldPng.data.length);
}
const diff = new pngjs_1.PNG({ width, height });
const numDiffPixels = (0, pixelmatch_1.default)(basePng.data, png.data, diff.data, width, height, options);
const diffPercentage = (numDiffPixels / (width * height)) * 100;
return {
error,
diffImage: pngjs_1.PNG.sync.write(diff),
diffPercentage,
diffPixels: numDiffPixels,
};
}
exports.pixelMatchDiff = pixelMatchDiff;
//# sourceMappingURL=pixelMatchDiff.js.map