diff --git a/jest-setup.js b/jest-setup.js index 624cfa0d392a3f0fe7e19e5b5c254c1b9ee2d484..1f7ad9b592c0c62a3f641a50d9ffde922e9d801d 100644 --- a/jest-setup.js +++ b/jest-setup.js @@ -1,26 +1,149 @@ -// 自动化测试 -const path = require('path'); -const { - configureToMatchImageSnapshot -} = require('jest-image-snapshot'); - -const hbuilderx_version = process.env.HX_Version -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}__` +const path = require("path"); +const fs = require("fs"); + +const { configureToMatchImageSnapshot } = require("jest-image-snapshot"); + +const hbuilderx_version = process.env.HX_Version; +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({ - toMatchImageSnapshot: configureToMatchImageSnapshot({ - customSnapshotIdentifier(args) { - return args.currentTestName.replace(/\//g, '-').replace(' ', '-'); - }, - customSnapshotsDir: path.join(__dirname, baseFolderName), - customDiffDir: path.join(__dirname, `${folderName}/`, 'diff'), - }), + toMatchImageSnapshot: configureToMatchImageSnapshot({ + customSnapshotIdentifier(args) { + return args.currentTestName.replace(/\//g, "-").replace(" ", "-"); + }, + customSnapshotsDir: path.join(__dirname, baseFolderName), + customDiffDir: path.join(__dirname, `${folderName}/`, "diff"), + }), + toSaveSnapshot, + toSaveImageSnapshot, }); + +function toSaveSnapshot(received, { customSnapshotsDir, fileName } = {}) { + const { testPath, currentTestName } = this; + const SNAPSHOTS_DIR = "__file_snapshots__"; + const snapshotDir = + process.env.saveSnapshotDir || + createSnapshotDir({ + customSnapshotsDir, + testPath, + SNAPSHOTS_DIR, + }); + const _fileName = createFileName({ + fileName, + testPath, + currentTestName, + }); + const filePath = path.join(snapshotDir, _fileName); + let message = () => `${currentTestName} toSaveSnapshot success`; + let pass = true; + + try { + checkSnapshotDir(snapshotDir); + fs.writeFileSync(filePath, received); + } catch (e) { + console.log("toSaveSnapshot fail", e); + message = () => e.message; + pass = false; + } + + return { + message, + pass, + }; +} + +function toSaveImageSnapshot( + received, + { customSnapshotsDir, customSnapshotIdentifier } = {} +) { + const { testPath, currentTestName } = this; + const SNAPSHOTS_DIR = "__image_snapshots__"; + const snapshotDir = + process.env.saveImageSnapshotDir || + createSnapshotDir({ + customSnapshotsDir, + testPath, + SNAPSHOTS_DIR, + }); + const _fileName = createFileName({ + fileName: customSnapshotIdentifier ? customSnapshotIdentifier() : "", + testPath, + currentTestName, + fileType: "png", + }); + const filePath = path.join(snapshotDir, _fileName); + let message = () => `${currentTestName} toSaveImageSnapshot success`; + let pass = true; + + try { + checkSnapshotDir(snapshotDir); + fs.writeFileSync(filePath, Buffer.from(received, "base64")); + } catch (e) { + console.log("toSaveImageSnapshot fail", e); + message = () => e.message; + pass = false; + } + + return { + message, + pass, + }; +} + +function createSnapshotDir({ customSnapshotsDir, testPath, SNAPSHOTS_DIR }) { + return customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR); +} + +function createFileName({ fileName, testPath, currentTestName, fileType }) { + return ( + fileName || + createSnapshotIdentifier({ + testPath, + currentTestName, + fileType, + }) + ); +} + +function createSnapshotIdentifier({ + testPath, + currentTestName, + fileType = "txt", +}) { + const snapshotIdentifier = kebabCase( + `${path.basename(testPath)}-${currentTestName}` + ); + const counter = timesCalled.get(`${snapshotIdentifier}-${fileType}`) || 1; + timesCalled.set(`${snapshotIdentifier}-${fileType}`, counter + 1); + return `${snapshotIdentifier}-${counter}.${fileType}`; +} + +function kebabCase(str) { + return str + .replaceAll(/([a-z])([A-Z])/g, "$1-$2") + .replaceAll(/\s+/g, "-") + .replaceAll(/_+/g, "-") + .replaceAll(/\/+/g, "-") + .replaceAll(/\.+/g, "-") + .toLowerCase(); +} + +function checkSnapshotDir(snapshotDir) { + if (!fs.existsSync(snapshotDir)) { + fs.mkdirSync(snapshotDir, { + recursive: true, + }); + } +} + +const timesCalled = new Map(); diff --git a/pages.json b/pages.json index 3da6ab1fd83ab12af9302f9dc7f493166b054a57..eb924922acaf71bd2349eef071b65025dcb22325 100644 --- a/pages.json +++ b/pages.json @@ -652,18 +652,18 @@ "navigationBarTitleText": "get-window-info" } }, - // #ifdef APP-ANDROID { - "path": "pages/API/facial-recognition-verify/facial-recognition-verify", + "path": "pages/API/element-draw/element-draw", "style": { - "navigationBarTitleText": "", + "navigationBarTitleText": "getDrawableContext", "enablePullDownRefresh": false } }, + // #ifdef APP-ANDROID { - "path": "pages/API/element-draw/element-draw", + "path": "pages/API/facial-recognition-verify/facial-recognition-verify", "style": { - "navigationBarTitleText": "getDrawableContext", + "navigationBarTitleText": "", "enablePullDownRefresh": false } }, @@ -1153,13 +1153,13 @@ } }, // #endif - // #ifdef APP-ANDROID { "path": "pages/template/calendar/calendar", "style": { "navigationBarTitleText": "日历" } }, + // #ifdef APP-ANDROID { "path": "pages/template/schema/schema", "style": { diff --git a/pages/component/scroll-view/scroll-view-custom-refresher-props.uvue b/pages/component/scroll-view/scroll-view-custom-refresher-props.uvue index a41dec907de727921fd1afa6d3f2dcb7e08284d0..43590788d7f175a922fe8bd3f9df6819ac9f9474 100644 --- a/pages/component/scroll-view/scroll-view-custom-refresher-props.uvue +++ b/pages/component/scroll-view/scroll-view-custom-refresher-props.uvue @@ -1,6 +1,6 @@