Car*_*arl 10 jestjs react-native
我在编写测试时迟到了(我还在学习!),并且在我的应用程序上运行 jest 测试时已经遇到了一些问题。特别是,使用反应导航。
现在我在运行 npm test 时遇到了如下问题
不变违规:未设置 __fbBatchedBridgeConfig,无法调用本机模块
Run Code Online (Sandbox Code Playgroud)at invariant (node_modules/invariant/invariant.js:40:15) at Object.invariant (node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:162:3) at Object.require [as NativeModules] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:322:12) at Object.NativeModules (node_modules/react-native-document-picker/index.js:3:30)
我遇到了一些其他问题,这些问题已按照下面显示的代码解决,但会产生此消息。
我的 package.json 如下:
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-navigation|react-navigation-redux-helpers|@react-navigation/.*)"
],
"setupFiles": [
"./node_modules/react-native-gesture-handler/jestSetup.js"
],
"transform": {
"^.+\\.js$": "babel-jest"
},
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./__mocks__/fileMock.js",
"\\.(css|scss)$": "./__mocks__/styleMock.js"
}
Run Code Online (Sandbox Code Playgroud)
jsetSetup 看起来像这样:
jest.mock('react-native-gesture-handler', () => {
const View = require('react-native/Libraries/Components/View/View');
return {
Swipeable: View,
DrawerLayout: View,
State: {},
ScrollView: View,
Slider: View,
Switch: View,
TextInput: View,
ToolbarAndroid: View,
ViewPagerAndroid: View,
DrawerLayoutAndroid: View,
WebView: View,
NativeViewGestureHandler: View,
TapGestureHandler: View,
FlingGestureHandler: View,
ForceTouchGestureHandler: View,
LongPressGestureHandler: View,
PanGestureHandler: View,
PinchGestureHandler: View,
RotationGestureHandler: View,
/* Buttons */
RawButton: View,
BaseButton: View,
RectButton: View,
BorderlessButton: View,
/* Other */
FlatList: View,
gestureHandlerRootHOC: jest.fn(),
Directions: {},
};
});
global.__fbBatchedBridgeConfig = require('./bridge-mock');
Run Code Online (Sandbox Code Playgroud)
我希望 npm test 运行测试并提供所需的输出。顺便说一句,我正在使用故事书/故事镜头。我很困惑为什么文档选择器组件在这里甚至是相关的,因为它没有在我使用的一个快照测试中被引用。
当存在一个或多个依赖项的符号链接时,可能会发生这种情况。您可以尝试以下操作吗?
yarn unlink <packageName> && yarn install --force
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4285 次 |
| 最近记录: |