我对编写测试来说有些晚了(我还在学习!),并且在我的应用上运行jest测试时遇到了一些问题,特别是与react-navigation相关的问题。
现在我正在运行npm test时遇到以下问题:
Invariant Violation: __fbBatchedBridgeConfig未设置,无法调用本机模块
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"
}
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');
我期望npm test运行测试并提供所需的输出。顺便说一句,我正在使用storybook / storyshot。 我对为什么在此处与文档选择器组件相关感到困惑,因为它没有在我使用的一个快照测试中被引用。