diff --git a/bin/rc-test-migrate.js b/bin/rc-test-migrate.js index 2457f98..f406adf 100755 --- a/bin/rc-test-migrate.js +++ b/bin/rc-test-migrate.js @@ -7,7 +7,9 @@ const fs = require("fs-extra"); const VER_FATHER = "father"; const RM_DEPS = ["jest", "father-build", "react-test-renderer"]; -const pkg = require(path.resolve(process.cwd(), "package.json")); +const cwd = process.cwd(); + +const pkg = require(path.resolve(cwd, "package.json")); // ================================================================== // Upgrade father version if exist @@ -21,7 +23,7 @@ if (fatherVer) { console.log("Override .fatherrc..."); fs.writeFileSync( - path.resolve(process.cwd(), ".fatherrc.js"), + path.resolve(cwd, ".fatherrc.js"), ` import { defineConfig } from 'father'; @@ -38,7 +40,7 @@ export default defineConfig({ ); // Clean up father v2 hooks - fs.removeSync(path.resolve(process.cwd(), ".git/hooks/pre-commit")); + fs.removeSync(path.resolve(cwd, ".git/hooks/pre-commit")); } } @@ -52,7 +54,7 @@ RM_DEPS.forEach((dep) => { }); fs.writeFileSync( - path.resolve(process.cwd(), "package.json"), + path.resolve(cwd, "package.json"), JSON.stringify(pkg, null, 2), "utf-8" ); @@ -64,6 +66,8 @@ console.log(" - 更新 .github/workflows 中 CI node 版本至 16"); console.log( " - 移除 jest.config.js 中关于 @testing-library/jsdom 的 setupFilesAfterEnv 配置" ); -console.log( - " - 重新安装依赖 node_modules" -); +console.log(" - 重新安装依赖 node_modules"); + +if (pkg.devDependencies["enzyme"]) { + console.log(" - (可选)移除 enzyme 测试,替换为 @testing-library/react"); +} diff --git a/package.json b/package.json index 95877cc..8654094 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "babel-plugin-module-resolver": "^4.1.0", "babel-preset-umi": "^1.8.4", "core-js": "^3.25.5", + "enzyme-adapter-react-16": "^1.15.6", "fs-extra": "^10.1.0", "jest": "^29.2.1", "jest-environment-jsdom": "^29.2.0", diff --git a/src/index.ts b/src/index.ts index 6967dd1..d429522 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,10 +9,19 @@ const ALIAS = { }; export function getConfig() { + const cwd = process.cwd(); + const pkg = require(path.resolve(cwd, 'package.json')); + + const setupFiles = [require.resolve('./setup.js')]; + + if (pkg.devDependencies['enzyme'] && !pkg.devDependencies['enzyme-adapter-react-16']) { + setupFiles.push(require.resolve('./setupEnzyme.js')); + } + const config = { - rootDir: process.cwd(), + rootDir: cwd, testEnvironment: 'jsdom', - setupFiles: [require.resolve('./setup.js')], + setupFiles, setupFilesAfterEnv: [require.resolve('./setupAfterEnv.js')], transform: { '\\.(t|j)sx?$': require.resolve('./transformers/jsTransformer'), diff --git a/src/setupEnzyme.ts b/src/setupEnzyme.ts new file mode 100644 index 0000000..135148a --- /dev/null +++ b/src/setupEnzyme.ts @@ -0,0 +1,4 @@ +const Enzyme = require('enzyme'); +const Adapter = require('enzyme-adapter-react-16'); + +Enzyme.configure({ adapter: new Adapter() });