提交 36a23a84 编写于 作者: dcloud_wdl's avatar dcloud_wdl

update jest-setup.js 适配新版截图对比方案

上级 76b14049
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const {
const { configureToMatchImageSnapshot } = require("jest-image-snapshot"); configureToMatchImageSnapshot
} = require('jest-image-snapshot');
const hbuilderx_version = process.env.HX_Version; let saveImageSnapshotDir = process.env.saveImageSnapshotDir || path.join(__dirname, '__snapshot__');
const uniTestPlatformInfo = process.env.uniTestPlatformInfo
? process.env.uniTestPlatformInfo.replace(/\s/g, "_")
: "";
const folderName = `__image_snapshots__/${hbuilderx_version}/__${uniTestPlatformInfo}__`;
let environment = "official";
if (hbuilderx_version.includes("dev")) {
environment = "dev";
} else if (hbuilderx_version.includes("alpha")) {
environment = "alpha";
}
const baseFolderName = `__image_snapshots__/base/${environment}/__${uniTestPlatformInfo}__`;
expect.extend({ expect.extend({
toMatchImageSnapshot: configureToMatchImageSnapshot({ toMatchImageSnapshot: configureToMatchImageSnapshot({
customSnapshotIdentifier(args) { customSnapshotIdentifier(args) {
return args.currentTestName.replace(/\//g, "-").replace(" ", "-"); return args.currentTestName.replace(/\//g, "-").replace(" ", "-");
}, },
customSnapshotsDir: path.join(__dirname, baseFolderName), customSnapshotsDir: process.env.saveImageSnapshotDir,
customDiffDir: path.join(__dirname, `${folderName}/`, "diff"), customDiffDir: path.join(saveImageSnapshotDir, "diff"),
}), }),
toSaveSnapshot, toSaveSnapshot,
toSaveImageSnapshot, toSaveImageSnapshot,
}); });
const testCaseToSnapshotFilePath = const testCaseToSnapshotFilePath =
process.env.testCaseToSnapshotFilePath || "./testCaseToSnapshotFilePath.json"; process.env.testCaseToSnapshotFilePath || "./testCaseToSnapshotFilePath.json";
if (!fs.existsSync(testCaseToSnapshotFilePath)) { if (!fs.existsSync(testCaseToSnapshotFilePath)) {
fs.writeFileSync(testCaseToSnapshotFilePath, "{}"); fs.writeFileSync(testCaseToSnapshotFilePath, "{}");
} }
function writeTestCaseToSnapshotFile(testCaseName, snapshotFilePath) { function writeTestCaseToSnapshotFile(testCaseName, snapshotFilePath) {
const data = JSON.parse(fs.readFileSync(testCaseToSnapshotFilePath)); const data = JSON.parse(fs.readFileSync(testCaseToSnapshotFilePath));
if (!data[testCaseName]) {
data[testCaseName] = [snapshotFilePath]; if (testCaseName.includes(__dirname)) {
} else { testCaseName = testCaseName.substring(`${__dirname}`.length);
data[testCaseName].push(snapshotFilePath); if (testCaseName[0] == '/' || testCaseName[0] == '\\') {
} testCaseName = testCaseName.substring(1);
fs.writeFileSync(testCaseToSnapshotFilePath, JSON.stringify(data, null, 2)); };
};
if (!data[testCaseName]) {
data[testCaseName] = [snapshotFilePath];
} else {
data[testCaseName].push(snapshotFilePath);
}
fs.writeFileSync(testCaseToSnapshotFilePath, JSON.stringify(data, null, 2));
} }
function toSaveSnapshot(received, { customSnapshotsDir, fileName } = {}) { function toSaveSnapshot(received, {
const { customSnapshotsDir,
snapshotState: { _rootDir }, fileName
testPath, } = {}) {
currentTestName, const {
} = this; snapshotState: {
const SNAPSHOTS_DIR = "__file_snapshots__"; _rootDir
const snapshotDir = },
process.env.saveSnapshotDir || testPath,
createSnapshotDir({ currentTestName,
customSnapshotsDir, } = this;
testPath, const SNAPSHOTS_DIR = "__file_snapshots__";
SNAPSHOTS_DIR, const snapshotDir =
process.env.saveSnapshotDir ||
createSnapshotDir({
customSnapshotsDir,
testPath,
SNAPSHOTS_DIR,
});
const _fileName = createFileName({
fileName,
testPath,
currentTestName,
}); });
const _fileName = createFileName({ const filePath = path.join(snapshotDir, _fileName);
fileName, let message = () => `${currentTestName} toSaveSnapshot success`;
testPath, let pass = true;
currentTestName,
}); try {
const filePath = path.join(snapshotDir, _fileName); checkSnapshotDir(snapshotDir);
let message = () => `${currentTestName} toSaveSnapshot success`; fs.writeFileSync(filePath, received);
let pass = true; writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath);
} catch (e) {
try { console.log("toSaveSnapshot fail", e);
checkSnapshotDir(snapshotDir); message = () => e.message;
fs.writeFileSync(filePath, received); pass = false;
writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath); }
} catch (e) {
console.log("toSaveSnapshot fail", e); return {
message = () => e.message; message,
pass = false; pass,
} };
return {
message,
pass,
};
} }
function toSaveImageSnapshot( function toSaveImageSnapshot(
received, received, {
{ customSnapshotsDir, customSnapshotIdentifier } = {} customSnapshotsDir,
customSnapshotIdentifier
} = {}
) { ) {
const { const {
snapshotState: { _rootDir }, snapshotState: {
testPath, _rootDir
currentTestName, },
} = this; testPath,
const SNAPSHOTS_DIR = "__image_snapshots__"; currentTestName,
const snapshotDir = } = this;
process.env.saveImageSnapshotDir || const SNAPSHOTS_DIR = "__image_snapshots__";
createSnapshotDir({ const snapshotDir =
customSnapshotsDir, process.env.saveImageSnapshotDir ||
testPath, createSnapshotDir({
SNAPSHOTS_DIR, customSnapshotsDir,
testPath,
SNAPSHOTS_DIR,
});
const _fileName = createFileName({
fileName: customSnapshotIdentifier ? customSnapshotIdentifier() : "",
testPath,
currentTestName,
fileType: "png",
}); });
const _fileName = createFileName({ const filePath = path.join(snapshotDir, _fileName);
fileName: customSnapshotIdentifier ? customSnapshotIdentifier() : "", let message = () => `${currentTestName} toSaveImageSnapshot success`;
testPath, let pass = true;
currentTestName,
fileType: "png", try {
}); checkSnapshotDir(snapshotDir);
const filePath = path.join(snapshotDir, _fileName); fs.writeFileSync(filePath, Buffer.from(received, "base64"));
let message = () => `${currentTestName} toSaveImageSnapshot success`; writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath);
let pass = true; } catch (e) {
console.log("toSaveImageSnapshot fail", e);
try { message = () => e.message;
checkSnapshotDir(snapshotDir); pass = false;
fs.writeFileSync(filePath, Buffer.from(received, "base64")); }
writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath);
} catch (e) { return {
console.log("toSaveImageSnapshot fail", e); message,
message = () => e.message; pass,
pass = false; };
}
return {
message,
pass,
};
} }
function createSnapshotDir({ customSnapshotsDir, testPath, SNAPSHOTS_DIR }) { function createSnapshotDir({
return customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR); customSnapshotsDir,
testPath,
SNAPSHOTS_DIR
}) {
return customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR);
} }
function createFileName({ fileName, testPath, currentTestName, fileType }) { function createFileName({
return ( fileName,
fileName || testPath,
createSnapshotIdentifier({ currentTestName,
testPath, fileType
currentTestName, }) {
fileType, return (
}) fileName ||
); createSnapshotIdentifier({
testPath,
currentTestName,
fileType,
})
);
} }
function createSnapshotIdentifier({ function createSnapshotIdentifier({
testPath, testPath,
currentTestName, currentTestName,
fileType = "txt", fileType = "txt",
}) { }) {
const snapshotIdentifier = kebabCase( const snapshotIdentifier = kebabCase(
`${path.basename(testPath)}-${currentTestName}` `${path.basename(testPath)}-${currentTestName}`
); );
const counter = timesCalled.get(`${snapshotIdentifier}-${fileType}`) || 1; const counter = timesCalled.get(`${snapshotIdentifier}-${fileType}`) || 1;
timesCalled.set(`${snapshotIdentifier}-${fileType}`, counter + 1); timesCalled.set(`${snapshotIdentifier}-${fileType}`, counter + 1);
return `${snapshotIdentifier}-${counter}.${fileType}`; return `${snapshotIdentifier}-${counter}.${fileType}`;
} }
function kebabCase(str) { function kebabCase(str) {
return str return str
.replaceAll(/([a-z])([A-Z])/g, "$1-$2") .replaceAll(/([a-z])([A-Z])/g, "$1-$2")
.replaceAll(/\s+/g, "-") .replaceAll(/\s+/g, "-")
.replaceAll(/_+/g, "-") .replaceAll(/_+/g, "-")
.replaceAll(/\/+/g, "-") .replaceAll(/\/+/g, "-")
.replaceAll(/\.+/g, "-") .replaceAll(/\.+/g, "-")
.toLowerCase(); .toLowerCase();
} }
function checkSnapshotDir(snapshotDir) { function checkSnapshotDir(snapshotDir) {
if (!fs.existsSync(snapshotDir)) { if (!fs.existsSync(snapshotDir)) {
fs.mkdirSync(snapshotDir, { fs.mkdirSync(snapshotDir, {
recursive: true, recursive: true,
}); });
} }
} }
const timesCalled = new Map(); const timesCalled = new Map();
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册