UNPKG

@johnf/react-native-owl

Version:
82 lines (80 loc) 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toMatchBaseline = void 0; var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _pixelmatch = _interopRequireDefault(require("pixelmatch")); var _pngjs = require("pngjs"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const toMatchBaseline = (latestPath, options = { threshold: 0.1, bottomCrop: 0 }) => { const platform = process.env.OWL_PLATFORM; const screenshotsDir = _path.default.join(_path.default.dirname(latestPath), '..', '..'); const baselinePath = _path.default.join(screenshotsDir, 'baseline', platform, _path.default.basename(latestPath)); if (latestPath === baselinePath) { return { message: () => 'Generated a fresh baseline, skipping comparison.', pass: true }; } try { const diffPath = _path.default.join(screenshotsDir, 'diff', platform, _path.default.basename(latestPath)); _fs.default.mkdirSync(_path.default.dirname(diffPath), { recursive: true }); const baselineData = _fs.default.readFileSync(baselinePath); const baselineImage = _pngjs.PNG.sync.read(baselineData); const croppedHeight = baselineImage.height - (options.bottomCrop || 0); const croppedBaselineImage = new _pngjs.PNG({ width: baselineImage.width, height: croppedHeight }); _pngjs.PNG.bitblt(baselineImage, croppedBaselineImage, 0, 0, baselineImage.width, croppedHeight, 0, 0); const latestData = _fs.default.readFileSync(latestPath); const latestImage = _pngjs.PNG.sync.read(latestData); const croppedLatestImage = new _pngjs.PNG({ width: baselineImage.width, height: croppedHeight }); _pngjs.PNG.bitblt(latestImage, croppedLatestImage, 0, 0, baselineImage.width, croppedHeight, 0, 0); const diffImage = new _pngjs.PNG({ width: baselineImage.width, height: croppedHeight }); const diffPixelsCount = (0, _pixelmatch.default)(croppedBaselineImage.data, croppedLatestImage.data, diffImage.data, baselineImage.width, croppedHeight, { threshold: options?.threshold }); if (diffPixelsCount === 0) { return { message: () => `Compared screenshot to match baseline. No differences were found.`, pass: true }; } // Create and save the diff image _fs.default.writeFileSync(diffPath, _pngjs.PNG.sync.write(diffImage), { encoding: 'binary' }); return { message: () => `Compared screenshot to match baseline. ${diffPixelsCount} were different.`, pass: diffPixelsCount === 0 }; } catch (error) { let message = 'Unknown error'; if (error instanceof Error) { message = error.message; } return { message: () => `Screenshot diffing error - ${message}`, pass: false }; } }; exports.toMatchBaseline = toMatchBaseline; expect.extend({ toMatchBaseline }); //# sourceMappingURL=matchers.js.map