我有一个Expo应用程序,并且正在使用SDK28。我的团队决定我们应该更新到最新版本,这意味着要更新React Native(因为最新的SDK使用RN 0.57)和Babel。
当我们更新依赖关系并修复配置文件时,Jest开始向我们显示此错误:
TypeError: Cannot read property 'fetch' of undefined
at node_modules/react-native/Libraries/vendor/core/whatwg-fetch.js:6:12
at Object.<anonymous> (node_modules/react-native/Libraries/vendor/core/whatwg-fetch.js:486:3)
at Object.<anonymous> (node_modules/jest-expo/src/setup.js:125:16)
经过几天的调试,我发现这与babel-jest
的预处理器无法正常工作有关,即使我遵循其安装docs。
我仔细研究了一下,发现此GitHub Issue thread中有一种解决方法。
实施变通办法,然后在我的babel.config.js
中添加babel-hoist,以使测试开始运行。
但是Jest的行为全都是不正确的,并且覆盖率数据不正确(即使我们已经对其进行了测试,它也会将某些行视为未发现)。
我想知道如何正确配置Jest以使其与Expo SDK 32兼容。
这些是相关的配置文件(设置为使用前面提到的解决方法)。
"dependencies": {
"@babel/preset-env": "^7.3.1",
"@expo/vector-icons": "6.3.1",
"expo": "^32.0.0",
"prop-types": "15.6.2",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"sentry-expo": "~1.9.0"
...
},
"devDependencies": {
"@babel/core": "^7.2.2",
"babel-eslint": "9.0.0",
"babel-plugin-jest-hoist": "^24.0.0",
"babel-preset-expo": "^5.0.0",
"enzyme": "3.8.0",
"enzyme-adapter-react-16": "^1.8.0",
"jest-expo": "^32.0.0",
"metro-react-native-babel-preset": "^0.51.1",
"react-dom": "^16.5.1",
...
},
"jest": {
"preset": "jest-expo",
"transform": {
"^.+\\.js$": "<rootDir>/jest.preprocessor.js"
},
"setupFiles": [
"<rootDir>/src/jest.setup.js"
],
...
}
*省略了一些依赖关系。
module.exports = {
presets: [
'babel-preset-expo',
'module:metro-react-native-babel-preset',
'module:react-native-dotenv',
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
sourceMaps: true,
plugins: [
'jest-hoist',
'@babel/transform-react-jsx-source',
],
};
答案 0 :(得分:2)
这是为我解决问题的原因:
yarn
。请按照此link的说明进行操作。package.json
看起来像这样:"dependencies": {
"@expo/vector-icons": "9.0.0",
"expo": "^32.0.0",
"prop-types": "15.6.2",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
...
},
"devDependencies": {
"babel-preset-expo": "^5.0.0",
"jest-expo": "^32.0.0",
...
}
"scripts": {
"test": "jest",
...
},
"jest": {
"preset": "jest-expo",
"transform": {
"^.+\\.js$": "babel-jest"
},
}
babel.config.js
。这是我的项目中运行Expo's SDK 32的项目之一:module.exports = function (api) {
api.cache(true);
return {
presets: [
'babel-preset-expo',
'module:react-native-dotenv',
],
sourceMaps: true,
plugins: [
'@babel/transform-react-jsx-source',
],
};
};
yarn
安装软件包(yarn install
)并运行测试yarn test
。答案 1 :(得分:1)
Expo会自动进行笑话的设置。 我认为您必须执行“ Expo init newProject”,然后阅读.babelrc和package.json
以下是expo init的结果。 效果很好。
// package.json
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject",
"test": "node ./node_modules/jest/bin/jest.js --watchAll"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"@expo/samples": "2.1.1",
"expo": "^32.0.0",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"react-navigation": "^3.0.9"
},
"devDependencies": {
"babel-preset-expo": "^5.0.0",
"jest-expo": "^32.0.0"
},
"private": true
}
// babel.config.js
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
};
};