无法在我的 react-native 项目上运行。
尝试在 react-native 项目上设置 jest 时,出现以下错误:
<project_root>\node_modules\react-native\Libraries\polyfills\error-guard.js:14
type ErrorHandler = (error: mixed, isFatal: boolean) => void;
^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at Runtime._execModule (node_modules/jest-runtime/build/index.js:1157:58)
at Object.<anonymous> (node_modules/react-native/jest/setup.js:16:6)
看来我的笑话运行 react-native's jest/setup.js
。并且由于“类型”(流关键字)而无法编译它的依赖关系。
我将 jest-expo(版本 42.0.1)与 babel-jest 一起使用,并按此处 babel-plugin-transform-flow-strip-types
所述添加。
我尝试了很多解决方案。一种是自己编译这些流文件,并使用 jestmoduleNameMapper
将它们替换为 react-native 的。但这引起了更多问题...
我开玩笑的配置package.json
:
"jest": {
"preset": "jest-expo",
"setupFilesAfterEnv": [
"@testing-library/jest-native/extend-expect"
],
"moduleNameMapper": {
"@react-native-firebase/analytics$": "<rootDir>/__mocks__/@react-native-firebase/analytics.js"
},
"transformIgnorePatterns": [
"rootDir>/node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|@sentry/.*)",
"<rootDir>/node_modules/(?!expo-image-manipulator|@react-native|@react-native-firebase/analytics|@unimodules/)",
"<rootDir>/node_modules/(?!expo-facebook)"
],
"collectCoverage": false,
"setupFiles": [
"./jestSetup.js"
],
"collectCoverageFrom": [
"**/*.{js,jsx}",
"!**/coverage/**",
"!**/node_modules/**",
"!**/babel.config.js",
"!**/jest.setup.js"
]
}
我的babel.config.js
:
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: ["@babel/plugin-transform-flow-strip-types"],
};
};
我会非常感谢你的帮助。