Shaq
Shaq

Reputation: 377

React Native Expo - Jest - React Native Firebase - Invariant Violation: Native module cannot be null

I try to run tests with jest but it breaks with an error:

Invariant Violation: Native module cannot be null.

      at invariant (node_modules/invariant/invariant.js:40:15)
      at RNFBNativeEventEmitter.NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:36:7)
      at new RNFBNativeEventEmitter (node_modules/@react-native-firebase/app/lib/internal/RNFBNativeEventEmitter.js:24:5)
      at Object.<anonymous> (node_modules/@react-native-firebase/app/lib/internal/RNFBNativeEventEmitter.js:48:16)
      at Object.<anonymous> (node_modules/@react-native-firebase/app/lib/internal/registry/nativeModule.js:21:1)

I use latest versions for RN, Expo, Typescript and others. My jest config is:

"jest": {
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|sentry-expo|native-base|@ui-kitten|@react-native-firebase/auth|@react-native-firebase/app)"
    ]
  },

If I remove @react-native-firebase from transformIgnorePatterns then test fails with error:

import { isAndroid, isBoolean } from '@react-native-firebase/app/lib/common';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import * as React from 'react';
    > 2 | import auth, { FirebaseAuthTypes } from '@react-native-firebase/auth';

Upvotes: 3

Views: 1937

Answers (1)

Karl Lopez
Karl Lopez

Reputation: 1099

Try to mock @react-native-firebase/your/needed/module

For example:

jest.mock('@react-native-firebase/app/lib/common', () => ({
   isAndroid: jest.fn(() => true),
   isBoolean: jest.fn(() => false),
}));

Upvotes: 3

Related Questions