Skip to content

Commit

Permalink
feat: 添加jest运行相关配置
Browse files Browse the repository at this point in the history
  • Loading branch information
yatessss committed Apr 18, 2023
1 parent e1e2700 commit 2f9bd60
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
4 changes: 0 additions & 4 deletions .babel.config.js

This file was deleted.

15 changes: 15 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -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 }],
],
};
4 changes: 4 additions & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -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'));
36 changes: 36 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -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: ['<rootDir>/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,
};
16 changes: 4 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
13 changes: 13 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 2f9bd60

Please sign in to comment.