提交 b54f8fe6 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

chore: toSaveSnapshot & toSaveImageSnapshot 时保存测试例与生成文件关系

上级 7e2f9bbd
...@@ -28,8 +28,29 @@ expect.extend({ ...@@ -28,8 +28,29 @@ expect.extend({
toSaveImageSnapshot, toSaveImageSnapshot,
}); });
const testCaseToSnapshotFilePath =
process.env.testCaseToSnapshotFilePath || "./testCaseToSnapshotFilePath.json";
if (!fs.existsSync(testCaseToSnapshotFilePath)) {
fs.writeFileSync(testCaseToSnapshotFilePath, "{}");
}
function writeTestCaseToSnapshotFile(testCaseName, snapshotFilePath) {
const data = JSON.parse(fs.readFileSync(testCaseToSnapshotFilePath));
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, { customSnapshotsDir, fileName } = {}) {
const { testPath, currentTestName } = this; const {
snapshotState: { _rootDir },
testPath,
currentTestName,
} = this;
const SNAPSHOTS_DIR = "__file_snapshots__"; const SNAPSHOTS_DIR = "__file_snapshots__";
const snapshotDir = const snapshotDir =
process.env.saveSnapshotDir || process.env.saveSnapshotDir ||
...@@ -50,6 +71,7 @@ function toSaveSnapshot(received, { customSnapshotsDir, fileName } = {}) { ...@@ -50,6 +71,7 @@ function toSaveSnapshot(received, { customSnapshotsDir, fileName } = {}) {
try { try {
checkSnapshotDir(snapshotDir); checkSnapshotDir(snapshotDir);
fs.writeFileSync(filePath, received); fs.writeFileSync(filePath, received);
writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath);
} catch (e) { } catch (e) {
console.log("toSaveSnapshot fail", e); console.log("toSaveSnapshot fail", e);
message = () => e.message; message = () => e.message;
...@@ -66,7 +88,11 @@ function toSaveImageSnapshot( ...@@ -66,7 +88,11 @@ function toSaveImageSnapshot(
received, received,
{ customSnapshotsDir, customSnapshotIdentifier } = {} { customSnapshotsDir, customSnapshotIdentifier } = {}
) { ) {
const { testPath, currentTestName } = this; const {
snapshotState: { _rootDir },
testPath,
currentTestName,
} = this;
const SNAPSHOTS_DIR = "__image_snapshots__"; const SNAPSHOTS_DIR = "__image_snapshots__";
const snapshotDir = const snapshotDir =
process.env.saveImageSnapshotDir || process.env.saveImageSnapshotDir ||
...@@ -88,6 +114,7 @@ function toSaveImageSnapshot( ...@@ -88,6 +114,7 @@ function toSaveImageSnapshot(
try { try {
checkSnapshotDir(snapshotDir); checkSnapshotDir(snapshotDir);
fs.writeFileSync(filePath, Buffer.from(received, "base64")); fs.writeFileSync(filePath, Buffer.from(received, "base64"));
writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath);
} catch (e) { } catch (e) {
console.log("toSaveImageSnapshot fail", e); console.log("toSaveImageSnapshot fail", e);
message = () => e.message; message = () => e.message;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册