Skip to content

Commit

Permalink
chore: adapter for enzyme
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Oct 21, 2022
1 parent 37e2c81 commit 5802493
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
18 changes: 11 additions & 7 deletions bin/rc-test-migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand All @@ -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"));
}
}

Expand All @@ -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"
);
Expand All @@ -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");
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 4 additions & 0 deletions src/setupEnzyme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');

Enzyme.configure({ adapter: new Adapter() });

0 comments on commit 5802493

Please sign in to comment.