From 3a718ecf95c4beec3cb7e677ca62431e2b5b94ef Mon Sep 17 00:00:00 2001 From: ZakaryCode Date: Sat, 6 May 2023 11:10:33 +0800 Subject: [PATCH] feat(jest): update snapshot resolver --- .husky/commit-msg | 0 .husky/pre-commit | 0 package.json | 1 + packages/create-app/src/createApp.ts | 0 packages/create-app/src/index.ts | 0 packages/jest-helper/.gitignore | 1 + packages/jest-helper/package.json | 23 ++++++ packages/jest-helper/src/sequencer.ts | 10 +++ packages/jest-helper/src/snapshot/resolver.ts | 24 +++++++ .../jest-helper/src/snapshot/serializers.ts | 55 ++++++++++++++ packages/jest-helper/tsconfig.json | 11 +++ packages/taro-cli-convertor/bin/taro-convert | 0 packages/taro-cli/bin/taro | 0 packages/taro-components/src/utils/index.ts | 0 .../release/swc_plugin_define_config.wasm | Bin .../{jest.config.js => jest.config.ts} | 9 ++- packages/taro-mini-runner/package.json | 1 + .../__snapshots__/alipay.spec.ts.snap | 3 - .../__snapshots__/babel.spec.ts.snap | 3 - .../__snapshots__/bytedance.spec.ts.snap | 4 -- .../__snapshots__/common-style.spec.ts.snap | 1 - .../compiler-macros.spec.ts.snap | 3 - .../__snapshots__/config.spec.ts.snap | 8 --- .../__snapshots__/css-modules.spec.ts.snap | 4 -- .../__snapshots__/custom-tabbar.spec.ts.snap | 22 +++--- .../__tests__/__snapshots__/jd.spec.ts.snap | 3 - .../__snapshots__/parse-html.spec.ts.snap | 3 - .../__snapshots__/prerender.spec.ts.snap | 9 --- .../__tests__/__snapshots__/qq.spec.ts.snap | 6 -- .../__snapshots__/react.spec.ts.snap | 3 - .../__tests__/__snapshots__/sass.spec.ts.snap | 5 -- .../__snapshots__/subpackages.spec.ts.snap | 2 - .../__tests__/__snapshots__/swan.spec.ts.snap | 3 - .../__snapshots__/tabbar.spec.ts.snap | 46 ++++++------ .../__tests__/__snapshots__/ts.spec.ts.snap | 2 - .../__tests__/__snapshots__/vue.spec.ts.snap | 2 - .../__tests__/__snapshots__/vue3.spec.ts.snap | 2 - .../__snapshots__/wx-hybrid.spec.ts.snap | 2 - .../fixtures/custom-tabbar/src/assets/nav.png | Bin .../custom-tabbar/src/assets/view.png | Bin .../fixtures/tabbar/src/assets/nav.png | Bin .../fixtures/tabbar/src/assets/view.png | Bin packages/taro-plugin-http/package.json | 2 +- .../{jest.config.js => jest.config.ts} | 9 ++- packages/taro-webpack-runner/package.json | 1 + .../__snapshots__/babel.spec.ts.snap | 2 - .../compiler-macros.spec.ts.snap | 2 - .../__snapshots__/config.spec.ts.snap | 6 -- .../__snapshots__/css-modules.spec.ts.snap | 2 - .../__tests__/__snapshots__/nerv.spec.ts.snap | 2 - .../__snapshots__/react.spec.ts.snap | 2 - .../__tests__/__snapshots__/sass.spec.ts.snap | 1 - .../__snapshots__/subpackages.spec.ts.snap | 2 - .../__tests__/__snapshots__/ts.spec.ts.snap | 1 - .../{jest.config.js => jest.config.ts} | 9 ++- packages/taro-webpack5-runner/package.json | 1 + .../__snapshots__/babel.spec.ts.snap | 4 -- .../compiler-macros.spec.ts.snap | 4 -- .../__snapshots__/config.spec.ts.snap | 14 ---- .../__snapshots__/css-modules.spec.ts.snap | 6 -- .../__snapshots__/framework.spec.ts.snap | 12 ---- .../__snapshots__/mini-platform.spec.ts.snap | 13 ---- .../__snapshots__/parse-html.spec.ts.snap | 3 - .../__snapshots__/prerender.spec.ts.snap | 9 --- .../__tests__/__snapshots__/sass.spec.ts.snap | 7 -- .../__snapshots__/subpackages.spec.ts.snap | 4 -- .../__snapshots__/tabbar.spec.ts.snap | 68 ++++++++---------- .../__tests__/__snapshots__/ts.spec.ts.snap | 3 - .../__snapshots__/wx-hybrid.spec.ts.snap | 2 - .../fixtures/custom-tabbar/src/assets/nav.png | Bin .../custom-tabbar/src/assets/view.png | Bin .../fixtures/tabbar/src/assets/nav.png | Bin .../fixtures/tabbar/src/assets/view.png | Bin pnpm-lock.yaml | 62 +++++++++------- 74 files changed, 247 insertions(+), 277 deletions(-) mode change 100755 => 100644 .husky/commit-msg mode change 100755 => 100644 .husky/pre-commit mode change 100755 => 100644 packages/create-app/src/createApp.ts mode change 100755 => 100644 packages/create-app/src/index.ts create mode 100644 packages/jest-helper/.gitignore create mode 100644 packages/jest-helper/package.json create mode 100644 packages/jest-helper/src/sequencer.ts create mode 100644 packages/jest-helper/src/snapshot/resolver.ts create mode 100644 packages/jest-helper/src/snapshot/serializers.ts create mode 100644 packages/jest-helper/tsconfig.json mode change 100755 => 100644 packages/taro-cli-convertor/bin/taro-convert mode change 100755 => 100644 packages/taro-cli/bin/taro mode change 100755 => 100644 packages/taro-components/src/utils/index.ts mode change 100755 => 100644 packages/taro-helper/swc/plugin-define-config/target/wasm32-wasi/release/swc_plugin_define_config.wasm rename packages/taro-mini-runner/{jest.config.js => jest.config.ts} (71%) mode change 100755 => 100644 packages/taro-mini-runner/src/__tests__/fixtures/custom-tabbar/src/assets/nav.png mode change 100755 => 100644 packages/taro-mini-runner/src/__tests__/fixtures/custom-tabbar/src/assets/view.png mode change 100755 => 100644 packages/taro-mini-runner/src/__tests__/fixtures/tabbar/src/assets/nav.png mode change 100755 => 100644 packages/taro-mini-runner/src/__tests__/fixtures/tabbar/src/assets/view.png rename packages/taro-webpack-runner/{jest.config.js => jest.config.ts} (76%) rename packages/taro-webpack5-runner/{jest.config.js => jest.config.ts} (79%) mode change 100755 => 100644 packages/taro-webpack5-runner/src/__tests__/fixtures/custom-tabbar/src/assets/nav.png mode change 100755 => 100644 packages/taro-webpack5-runner/src/__tests__/fixtures/custom-tabbar/src/assets/view.png mode change 100755 => 100644 packages/taro-webpack5-runner/src/__tests__/fixtures/tabbar/src/assets/nav.png mode change 100755 => 100644 packages/taro-webpack5-runner/src/__tests__/fixtures/tabbar/src/assets/view.png diff --git a/.husky/commit-msg b/.husky/commit-msg old mode 100755 new mode 100644 diff --git a/.husky/pre-commit b/.husky/pre-commit old mode 100755 new mode 100644 diff --git a/package.json b/package.json index 091e577aa81b..af9db84618cf 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "lint": "eslint packages/ --ext .js --ext .ts --ext .tsx", "lint:style": "stylelint ./packages/**/*.{css,scss}", "test": "pnpm --if-present -r --aggregate-output --filter=./packages/* test:ci", + "updateSnapshot": "pnpm --if-present -r --aggregate-output --filter=./packages/* updateSnapshot", "version": "run-s version:*", "version:release": "pnpm --parallel -r --aggregate-output --filter=./packages/* exec npm version ${npm_package_version}", "version:git": "git add . && git commit -m \"chore(release): publish ${npm_package_version}\"", diff --git a/packages/create-app/src/createApp.ts b/packages/create-app/src/createApp.ts old mode 100755 new mode 100644 diff --git a/packages/create-app/src/index.ts b/packages/create-app/src/index.ts old mode 100755 new mode 100644 diff --git a/packages/jest-helper/.gitignore b/packages/jest-helper/.gitignore new file mode 100644 index 000000000000..a65b41774ad5 --- /dev/null +++ b/packages/jest-helper/.gitignore @@ -0,0 +1 @@ +lib diff --git a/packages/jest-helper/package.json b/packages/jest-helper/package.json new file mode 100644 index 000000000000..bc338d586a96 --- /dev/null +++ b/packages/jest-helper/package.json @@ -0,0 +1,23 @@ +{ + "name": "jest-taro-helper", + "version": "3.6.6", + "description": "jest helper for taro", + "private": true, + "main": "index.js", + "files": [ + "lib" + ], + "scripts": { + "build": "tsc", + "dev": "tsc -w" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "@jest/test-sequencer": "^29.5.0", + "jest-runner": "^29.5.0", + "typescript": "^4.7.4", + "pretty-format": "^29.5.0" + } +} diff --git a/packages/jest-helper/src/sequencer.ts b/packages/jest-helper/src/sequencer.ts new file mode 100644 index 000000000000..cddc40cb46b2 --- /dev/null +++ b/packages/jest-helper/src/sequencer.ts @@ -0,0 +1,10 @@ +import type { Test } from 'jest-runner' + +const Sequencer = require('@jest/test-sequencer').default + +export default class CustomSequencer extends Sequencer { + sort (tests: Test[]) { + const copyTests = Array.from(tests) + return copyTests.sort((testA, testB) => testA.path.localeCompare(testB.path)) + } +} diff --git a/packages/jest-helper/src/snapshot/resolver.ts b/packages/jest-helper/src/snapshot/resolver.ts new file mode 100644 index 000000000000..932eb603ca35 --- /dev/null +++ b/packages/jest-helper/src/snapshot/resolver.ts @@ -0,0 +1,24 @@ +import * as path from 'path' + +const TEST_DIR = '__tests__' +const SNAPSHOT_DIR = '__snapshots__' + +export const resolveSnapshotPath = (testPath: string, snapshotExtension: string) => { + return testPath.replace(TEST_DIR, SNAPSHOT_DIR) + snapshotExtension +} + +export const resolveTestPath = (snapshotPath: string, snapshotExtension: string) => { + return snapshotPath.replace(snapshotExtension, '').replace(SNAPSHOT_DIR, TEST_DIR) +} + +export const testPathForConsistencyCheck = path.posix.join( + 'consistency_check', + TEST_DIR, + 'example.test.js' +) + +export default { + resolveSnapshotPath, + resolveTestPath, + testPathForConsistencyCheck +} diff --git a/packages/jest-helper/src/snapshot/serializers.ts b/packages/jest-helper/src/snapshot/serializers.ts new file mode 100644 index 000000000000..72e3c6b93843 --- /dev/null +++ b/packages/jest-helper/src/snapshot/serializers.ts @@ -0,0 +1,55 @@ +import * as os from 'os' +// import type { Config, Refs, Printer } from 'pretty-format' + +export const print = (val: string) => { + // Note: 对齐各平台的路径分隔符 + return val.replace(/\\*\*\sfilePath:\s(.*)\s\*\*\//g, (replaceValue) => replaceValue.replace(/\\/g, '/')) +} + +export const parseSnapshotByFilePath = (val: string) => { + const arr = val.split(new RegExp(os.EOL + '|\n')) + let key = '' + return arr.reduce((acc, cur) => { + if (cur.startsWith('/** filePath:')) { + key = cur.replace(/\\/g, '/') + acc[key] = '' + } else { + acc[key] ||= '' + if (acc[key] !== '') { + acc[key] += '\n' + } + acc[key] += cur + } + return acc + }, {}) +} + +export const snapshotObject2String = (val: Record) => { + return `"\n${Object.entries(val) + .sort(([key1], [key2]) => key1.localeCompare(key2)) + .filter(([key]) => key !== '') + .map(([key, value]) => `${key}\n${value}`) + .join('\n')}"` +} + +export const serialize = ( + val: any, + // config: Config, + // indentation: string, + // depth: number, + // refs: Refs, + // printer: Printer, +) => { + if (typeof val === 'string') { + return snapshotObject2String(parseSnapshotByFilePath(val)) + } + return val +} + +export const test = (val: unknown) => typeof val === 'string' + +export default { + // print, + serialize, + test, +} diff --git a/packages/jest-helper/tsconfig.json b/packages/jest-helper/tsconfig.json new file mode 100644 index 000000000000..90ec8083ddee --- /dev/null +++ b/packages/jest-helper/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.root.json", + "compilerOptions": { + "baseUrl": ".", + "outDir": "lib", + "rootDir": "./src", + "module": "commonjs" + }, + "include": ["./src"], + "exclude": ["./src/__tests__"] +} diff --git a/packages/taro-cli-convertor/bin/taro-convert b/packages/taro-cli-convertor/bin/taro-convert old mode 100755 new mode 100644 diff --git a/packages/taro-cli/bin/taro b/packages/taro-cli/bin/taro old mode 100755 new mode 100644 diff --git a/packages/taro-components/src/utils/index.ts b/packages/taro-components/src/utils/index.ts old mode 100755 new mode 100644 diff --git a/packages/taro-helper/swc/plugin-define-config/target/wasm32-wasi/release/swc_plugin_define_config.wasm b/packages/taro-helper/swc/plugin-define-config/target/wasm32-wasi/release/swc_plugin_define_config.wasm old mode 100755 new mode 100644 diff --git a/packages/taro-mini-runner/jest.config.js b/packages/taro-mini-runner/jest.config.ts similarity index 71% rename from packages/taro-mini-runner/jest.config.js rename to packages/taro-mini-runner/jest.config.ts index 8a243c0af8ff..698bee23b830 100644 --- a/packages/taro-mini-runner/jest.config.js +++ b/packages/taro-mini-runner/jest.config.ts @@ -1,7 +1,10 @@ -module.exports = { +import type { Config } from 'jest' + +const config: Config = { moduleFileExtensions: ['ts', 'tsx', 'js', 'json', 'jsx', 'node'], preset: 'ts-jest', - setupFilesAfterEnv: ['./src/__tests__/setup/index.ts'], + setupFilesAfterEnv: ['/src/__tests__/setup/index.ts'], + snapshotSerializers: ['jest-taro-helper/lib/snapshot/serializers.js'], testEnvironment: 'node', testEnvironmentOptions: {}, testMatch: ['**/__tests__/?(*.)+(spec|test).[jt]s?(x)'], @@ -20,3 +23,5 @@ module.exports = { 'node_modules', ], } + +export default config diff --git a/packages/taro-mini-runner/package.json b/packages/taro-mini-runner/package.json index c5ce172f1462..c0a1949b1ede 100644 --- a/packages/taro-mini-runner/package.json +++ b/packages/taro-mini-runner/package.json @@ -94,6 +94,7 @@ "babel-jest": "^29.5.0", "jest": "^29.3.1", "jest-cli": "^29.3.1", + "jest-taro-helper": "workspace:*", "jest-transform-css": "^6.0.1", "jest-environment-node": "^29.5.0", "memfs": "^3.1.2", diff --git a/packages/taro-mini-runner/src/__tests__/__snapshots__/alipay.spec.ts.snap b/packages/taro-mini-runner/src/__tests__/__snapshots__/alipay.spec.ts.snap index 2729c14743f7..7930307eaa49 100644 --- a/packages/taro-mini-runner/src/__tests__/__snapshots__/alipay.spec.ts.snap +++ b/packages/taro-mini-runner/src/__tests__/__snapshots__/alipay.spec.ts.snap @@ -6,7 +6,6 @@ exports[`alipay should build alipay app 2`] = ` " /** filePath: dist/app.acss **/ - /** filePath: dist/app.js **/ require("./runtime"); @@ -1012,7 +1011,6 @@ require("./taro"); /** filePath: dist/pages/index/index.acss **/ - /** filePath: dist/pages/index/index.axml **/