creevey
Version:
Cross-browser screenshot testing tool for Storybook with fancy UI Runner
53 lines • 2.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.update = update;
const path_1 = __importDefault(require("path"));
const fs_1 = require("fs");
const micromatch_1 = __importDefault(require("micromatch"));
const types_js_1 = require("../types.js");
const utils_js_1 = require("./utils.js");
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_js_1.isDefined)
.filter(([fileName, imageName]) => !testPaths || (testPaths.find(([token]) => token == imageName) && isMatch(path_1.default.join(srcPath, fileName))))
.reduce((images, [, imageName, retry]) => Number(retry) > (images.get(imageName) ?? -1) ? images.set(imageName, Number(retry)) : images, new Map())
.forEach((retry, imageName) => {
(0, fs_1.mkdirSync)(dstPath, { recursive: true });
(0, fs_1.copyFileSync)(path_1.default.join(srcPath, `${imageName}-actual-${retry}.png`), path_1.default.join(dstPath, `${imageName}.png`));
});
}
function traverse(srcPath, dstPath, testPaths, isMatch) {
const dirents = (0, fs_1.readdirSync)(srcPath, { withFileTypes: true });
approve(dirents, srcPath, dstPath, testPaths, isMatch);
dirents
.filter((dirent) => dirent.isDirectory())
.map((dirent) => [
dirent.name,
testPaths?.map(([token, ...restPath]) => (token == dirent.name ? restPath : null)).filter(types_js_1.isDefined),
])
.filter(([, paths]) => !paths || paths.length > 0)
.forEach(([dirname, paths]) => {
traverse(path_1.default.join(srcPath, dirname), path_1.default.join(dstPath, dirname), paths, isMatch);
});
}
function update(config, grepPattern) {
const { reportDir, screenDir } = config;
const isMatch = grepPattern ? micromatch_1.default.matcher(grepPattern, { contains: true }) : () => true;
const testsMeta = (0, utils_js_1.tryToLoadTestsData)(`${reportDir}/tests.json`);
const testsReport = (0, utils_js_1.tryToLoadTestsData)(`${reportDir}/data.js`);
let testPaths = null;
if (testsMeta && testsReport) {
testPaths = Object.values(testsMeta)
.filter(types_js_1.isDefined)
.filter(({ id }) => testsReport[id]?.status == 'failed')
.map(({ storyPath, testName, browser }) => [...storyPath, ...(testName ? [testName] : []), browser]);
}
traverse(reportDir, screenDir, testPaths, (value) => isMatch(path_1.default.relative(reportDir, value)));
}
//# sourceMappingURL=update.js.map