creevey
Version:
Cross-browser screenshot testing tool for Storybook with fancy UI Runner
78 lines (60 loc) • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = update;
var _path = _interopRequireDefault(require("path"));
var _fs = _interopRequireWildcard(require("fs"));
var _micromatch = _interopRequireDefault(require("micromatch"));
var _types = require("../types");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function tryToLoadTestsMeta(reportDir) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(`${reportDir}/tests.json`);
} catch (_) {
/* noop */
}
}
const actualRegex = /^(.*)-actual-(\d+)\.png$/i;
function approve(dirents, srcPath, dstPath, testPaths, isMatch) {
dirents.filter(dirent => dirent.isFile()).map(dirent => actualRegex.exec(dirent.name)).filter(_types.isDefined).filter(([fileName, imageName]) => (testPaths === null || testPaths === void 0 ? void 0 : testPaths.find(([token]) => token == imageName)) && isMatch(_path.default.join(srcPath, fileName))).reduce((images, [, imageName, retry]) => {
var _images$get;
return Number(retry) > ((_images$get = images.get(imageName)) !== null && _images$get !== void 0 ? _images$get : -1) ? images.set(imageName, Number(retry)) : images;
}, new Map()).forEach((retry, imageName) => {
(0, _fs.mkdirSync)(dstPath, {
recursive: true
});
_fs.default.copyFileSync(_path.default.join(srcPath, `${imageName}-actual-${retry}.png`), _path.default.join(dstPath, `${imageName}.png`));
});
}
function traverse(srcPath, dstPath, testPaths, isMatch) {
const dirents = _fs.default.readdirSync(srcPath, {
withFileTypes: true
});
approve(dirents, srcPath, dstPath, testPaths, isMatch);
dirents.filter(dirent => dirent.isDirectory()).map(dirent => [dirent.name, testPaths === null || testPaths === void 0 ? void 0 : testPaths.map(([token, ...restPath]) => token == dirent.name ? restPath : null).filter(_types.isDefined)]).filter(([, paths]) => !paths || paths.length > 0).forEach(([dirname, paths]) => traverse(_path.default.join(srcPath, dirname), _path.default.join(dstPath, dirname), paths, isMatch));
}
function update(config, grepPattern) {
const {
reportDir,
screenDir
} = config;
const isMatch = grepPattern ? _micromatch.default.matcher(grepPattern, {
contains: true
}) : () => true;
const tests = tryToLoadTestsMeta(reportDir);
let testPaths = null;
if (tests) {
testPaths = Object.values(tests).filter(_types.isDefined).filter(({
status
}) => status == 'failed').map(({
storyPath,
testName,
browser
}) => [...storyPath, ...(testName ? [testName] : []), browser]);
}
traverse(reportDir, screenDir, testPaths, value => isMatch(_path.default.relative(reportDir, value)));
}