Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fbBatchedBridgeConfig is not set when running jest tests

I have come late to the party on writing tests (I am still learning!), and have already had a few issues running jest tests on my app. In particular, with react-navigation.

Now I am having an issue as per the below when I run npm test

Invariant Violation: __fbBatchedBridgeConfig is not set, cannot invoke native modules

 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)

I have had some other problems which have been solved as per the code shown below, but give rise to this message.

My package.json is as follows:

"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 looks like this:

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');

I expect npm test to run the tests and provide the required output. As an aside, I'm using storybook/storyshot. I'm confused as to why the document-picker component is even relevant here, as it is not referenced in the one snapshot test I am using.

like image 411
Carl Avatar asked Sep 13 '25 02:09

Carl


1 Answers

This can happen when there are symlinks for one or more dependencies. Could you please try the following?

yarn unlink <packageName> && yarn install --force 
like image 118
Omer Gurarslan Avatar answered Sep 16 '25 07:09

Omer Gurarslan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!