Skip to content

Commit 5802493

Browse files
committed
chore: adapter for enzyme
1 parent 37e2c81 commit 5802493

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

bin/rc-test-migrate.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const fs = require("fs-extra");
77
const VER_FATHER = "father";
88
const RM_DEPS = ["jest", "father-build", "react-test-renderer"];
99

10-
const pkg = require(path.resolve(process.cwd(), "package.json"));
10+
const cwd = process.cwd();
11+
12+
const pkg = require(path.resolve(cwd, "package.json"));
1113

1214
// ==================================================================
1315
// Upgrade father version if exist
@@ -21,7 +23,7 @@ if (fatherVer) {
2123

2224
console.log("Override .fatherrc...");
2325
fs.writeFileSync(
24-
path.resolve(process.cwd(), ".fatherrc.js"),
26+
path.resolve(cwd, ".fatherrc.js"),
2527
`
2628
import { defineConfig } from 'father';
2729
@@ -38,7 +40,7 @@ export default defineConfig({
3840
);
3941

4042
// Clean up father v2 hooks
41-
fs.removeSync(path.resolve(process.cwd(), ".git/hooks/pre-commit"));
43+
fs.removeSync(path.resolve(cwd, ".git/hooks/pre-commit"));
4244
}
4345
}
4446

@@ -52,7 +54,7 @@ RM_DEPS.forEach((dep) => {
5254
});
5355

5456
fs.writeFileSync(
55-
path.resolve(process.cwd(), "package.json"),
57+
path.resolve(cwd, "package.json"),
5658
JSON.stringify(pkg, null, 2),
5759
"utf-8"
5860
);
@@ -64,6 +66,8 @@ console.log(" - 更新 .github/workflows 中 CI node 版本至 16");
6466
console.log(
6567
" - 移除 jest.config.js 中关于 @testing-library/jsdom 的 setupFilesAfterEnv 配置"
6668
);
67-
console.log(
68-
" - 重新安装依赖 node_modules"
69-
);
69+
console.log(" - 重新安装依赖 node_modules");
70+
71+
if (pkg.devDependencies["enzyme"]) {
72+
console.log(" - (可选)移除 enzyme 测试,替换为 @testing-library/react");
73+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"babel-plugin-module-resolver": "^4.1.0",
4444
"babel-preset-umi": "^1.8.4",
4545
"core-js": "^3.25.5",
46+
"enzyme-adapter-react-16": "^1.15.6",
4647
"fs-extra": "^10.1.0",
4748
"jest": "^29.2.1",
4849
"jest-environment-jsdom": "^29.2.0",

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@ const ALIAS = {
99
};
1010

1111
export function getConfig() {
12+
const cwd = process.cwd();
13+
const pkg = require(path.resolve(cwd, 'package.json'));
14+
15+
const setupFiles = [require.resolve('./setup.js')];
16+
17+
if (pkg.devDependencies['enzyme'] && !pkg.devDependencies['enzyme-adapter-react-16']) {
18+
setupFiles.push(require.resolve('./setupEnzyme.js'));
19+
}
20+
1221
const config = {
13-
rootDir: process.cwd(),
22+
rootDir: cwd,
1423
testEnvironment: 'jsdom',
15-
setupFiles: [require.resolve('./setup.js')],
24+
setupFiles,
1625
setupFilesAfterEnv: [require.resolve('./setupAfterEnv.js')],
1726
transform: {
1827
'\\.(t|j)sx?$': require.resolve('./transformers/jsTransformer'),

src/setupEnzyme.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const Enzyme = require('enzyme');
2+
const Adapter = require('enzyme-adapter-react-16');
3+
4+
Enzyme.configure({ adapter: new Adapter() });

0 commit comments

Comments
 (0)