【发布时间】:2017-05-22 06:54:14
【问题描述】:
我无法让 Jest 与 react-navigation 一起工作。我按照react-navigation guidelines 中的说明创建了一个新项目,然后添加了 Jest 并按照下面的详细说明进行了配置。该应用程序运行良好,但是当我运行测试时遇到以下错误:
React caught an error thrown by NavigationContainer. You should fix this error in your code. Consider adding an error boundary to your tree to customize error handling behavior.
TypeError: Cannot read property 'then' of undefined
The error is located at:
in NavigationContainer (created by App)
in App
The error was thrown at:
at NavigationContainer.componentDidMount (/Users/gustav/kicksort/albert/albertReact1/node_modules/react-navigation/src/createNavigationContainer.js:189:2171)
at commitLifeCycles (/Users/gustav/kicksort/albert/albertReact1/node_modules/react-test-renderer/lib/ReactFiberCommitWork.js:421:24),
...
...
以下是我的 package.json 文件的相关内容:
"dependencies": {
"react": "16.0.0-alpha.6",
"react-dom": "16.0.0-alpha.6",
"react-native": "0.44.0",
"react-navigation": "1.0.0-beta.9"
},
"devDependencies": {
"babel-jest": "20.0.3",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
"babel-preset-react-native": "^1.9.2",
"enzyme": "2.8.2",
"jest": "20.0.3",
"jest-cli": "20.0.3",
"jest-react-native": "18.0.0",
"react-test-renderer": "16.0.0-alpha.6"
},
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-navigation)"
]
}
这是我的 .babelrc 文件:
{
"presets": ["react-native"],
"env": {
"test": {
"presets": ["react-native"],
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
最后是 __mocks__/react-native.js:
const rn = require('react-native')
jest.mock('Linking', () => {
return {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
openURL: jest.fn(),
canOpenURL: jest.fn(),
getInitialURL: jest.fn(),
}
})
module.exports = rn;
【问题讨论】:
标签: react-native jestjs react-navigation babel-jest