我在编写测试时迟到了(我还在学习!),并且在我的应用程序上运行 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: …Run Code Online (Sandbox Code Playgroud)