目前我正在使用 jest 对 react native 应用程序(expo CLI)进行单元测试,但是在测试渲染时,当我从 react-native 导入任何组件时,它完全没问题,但如果我从其他(例如 react-native-ui -小猫)。会有如下图所示的错误。有人可以帮助我提前谢谢!
我在 package.json 中配置了“transformIgnorePatterns”但不工作
我的 package.json
"jest": {
"transform": {
"^.+\\.(tsx|ts)?$": "ts-jest"
},
"preset": "jest-expo",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
]
},
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|@ui-kitten/components|react-native-ui-kitten|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base)"
],
"husky": {
"hooks": {
"pre-commit": "npm run lint",
"pre-push": "npm run lint"
}
}
我的 jest.config.js
const {defaults} = require('jest-config');
module.exports = {
// ...
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx'],
preset: "jest-expo",
// ...
};
我的 babel.config.js
const path = require('path');
const Config = require('./config');
// FIXME: Resolve `transform[stderr]: Could not resolve` command-line warnings.
// FIXME: Reproducible when starting with clearing cache (npm start -- -c)
//
// TODO: Framework path aliasing even not needed here. Replace?
// TODO: Replace nested package.json-s with aliases
const moduleResolverConfig = {
root: path.resolve('./'),
alias: {
'@kitten/theme': path.resolve(Config.KITTEN_PATH, 'theme'),
'@kitten/ui': path.resolve(Config.KITTEN_PATH, 'ui'),
'@eva-design/eva': path.resolve(Config.MAPPING_PATH),
'@eva-design/processor': path.resolve(Config.PROCESSOR_PATH),
},
};
module.exports = function (api) {
api.cache(true);
const presets = [
'babel-preset-expo',
];
const plugins = [
['module-resolver', moduleResolverConfig],
];
return { presets, plugins };
};