diff --git a/.babel.config.js b/.babel.config.js deleted file mode 100644 index 4265acc..0000000 --- a/.babel.config.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'], - plugins: ['react-native-reanimated/plugin', '@babel/plugin-transform-runtime'], -}; diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 0000000..ed3a006 --- /dev/null +++ b/babel.config.js @@ -0,0 +1,15 @@ +module.exports = { + presets: [ + 'module:metro-react-native-babel-preset', + '@babel/preset-env', + '@babel/preset-react', + '@babel/preset-typescript', + ], + plugins: [ + 'react-native-reanimated/plugin', + '@babel/plugin-transform-runtime', + ['@babel/plugin-proposal-private-methods', { loose: true }], + ['@babel/plugin-proposal-private-property-in-object', { loose: true }], + ['@babel/plugin-proposal-class-properties', { loose: true }], + ], +}; diff --git a/jest-setup.js b/jest-setup.js new file mode 100644 index 0000000..c419e89 --- /dev/null +++ b/jest-setup.js @@ -0,0 +1,4 @@ +// require('react-native-reanimated/lib/commonjs/reanimated2/jestUtils').setUpTests(); + +// eslint-disable-next-line global-require +jest.mock('react-native-reanimated', () => require('react-native-reanimated/mock')); diff --git a/jest.config.js b/jest.config.js index 4ee5f16..3bb57bd 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,4 +1,40 @@ +const { defaults } = require('ts-jest/presets'); + module.exports = { + // ts的preset + ...defaults, + // Jest 配置基础的预设 preset: 'react-native', + + // 模块使用的文件扩展名数组 moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], + + // 检测测试文件的模式(tests 目录下的 tsx 或 jsx 文件) + testRegex: '(src/components/.*\\.(test|spec))\\.[tj]sx?$', + + // 匹配的路径下文件将跳过覆盖率信息 + testPathIgnorePatterns: ['/node_modules/', '\\.snap$'], + + // 定义文件的编译方式 + transform: { + '^.+\\.tsx?$': [ + 'ts-jest', + { + babelConfig: true, + // tsconfig适配node modules下的一些文件 + tsconfig: './tsconfig.test.json', + }, + ], + }, + + // 定义了忽略进行 jest 执行的依赖包 + transformIgnorePatterns: [ + 'node_modules/(?!(react-native|@react-native|@testing-library|react-navigation|@react-navigation/.*|@react-native-community|react-native-reanimated)/)', + ], + + // react-native-reanimated运行时需要一些配置 + setupFilesAfterEnv: ['./jest-setup.js'], + + // 使用工作线程进行并行化 + workerThreads: true, }; diff --git a/package.json b/package.json index 0d69ee9..637365b 100644 --- a/package.json +++ b/package.json @@ -51,26 +51,18 @@ "eslint-config-react-app": "^7.0.1", "eslint-plugin-import": "^2.22.0", "husky": "^8.0.3", + "jest": "^29.5.0", "lint-staged": "^13.1.0", "prettier": "^2.8.3", "prismjs": "^1.29.0", - "react-router-dom": "^6.8.0" + "react-router-dom": "^6.8.0", + "react-test-renderer": "^18.2.0", + "ts-jest": "^29.1.0" }, "peerDependencies": { "eslint-config-react-app": "^7.0.1", "react": "18.2.0", "react-dom": "18.2.0", "react-native": "0.71.3" - }, - "jest": { - "preset": "react-native", - "moduleFileExtensions": [ - "ts", - "tsx", - "js", - "jsx", - "json", - "node" - ] } } diff --git a/tsconfig.test.json b/tsconfig.test.json new file mode 100644 index 0000000..d2cc505 --- /dev/null +++ b/tsconfig.test.json @@ -0,0 +1,13 @@ +/* + * @Author: yatessss + * @Date: 2023-04-18 15:05:28 + * @LastEditors: yatessss + * @LastEditTime: 2023-04-18 15:05:37 + * @Description: 用于jest的 tsconfig + */ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "isolatedModules": false + } +} \ No newline at end of file