Error when implementing unit tests in React Native with Reactnavigation

0

I'm having the following problem:

  

Invariant Violation: Native module can not be null.

  at invariant (node_modules/fbjs/lib/invariant.js:42:15)
  at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:35:7)
  at Object.<anonymous> (node_modules/react-native-google-analytics-bridge/src/Helpers/FunctionCallTagHandler/index.io
     

s.js: 16: 37)         at Object. (node_modules / react-native-google-analytics-bridge / src / GoogleTagManager.js: 2: 1)

I'm trying to test the login of an app, of which it has the root component, from which it checks the status and depending on whether it is logged em> or not open the login or the user's main screen. Would anyone know how to solve it?

    
asked by Gaston Diaz 17.07.2018 в 21:38
source

1 answer

0

The solution was given to me from stack overflow in English by the user

and consists of modifying the package.json in the jest part, leaving it as follows:

"jest": {
        "preset": "react-native",
        "haste": {
          "defaultPlatform": "android",
          "platforms": [
            "android",
            "ios"
          ],
          "providesModuleNodeModules": [
            "react",
            "react-native",
            "react-native-push-notification"
          ]
        },
        "collectCoverageFrom": [
            "src/**/*.js"
        ],
        "coverageReporters": [
            "html",
            "text"
        ],
        "transformIgnorePatterns": [
            "node_modules/(?!(jest-)?react-native|react-navigation)"
        ],
        "setupFiles": [
            "<rootDir>/__mock__/pushNotificationIOS.js"
        ]
      }

and create in the root of the project a folder called __mock__ that would contain the file pushNotificationIOS.js with the following code:

const PushNotificationIOSMock = jest.mock('react-native-push-notification', () => ({
    configure: jest.fn(),
    onRegister: jest.fn(),
    onNotification: jest.fn(),
    addEventListener: jest.fn(),
    requestPermissions: jest.fn(() => Promise.resolve()),
    getInitialNotification: jest.fn(() => Promise.resolve()),
}));

global.PushNotificationIOS = PushNotificationIOSMock;

and in the test file import

import {GoogleAnalyticsTracker} from 'react-native-google-analytics-bridge';

jest.mock('react-native-google-analytics-bridge');
    
answered by 17.07.2018 в 22:16