TypeError: Super expression must either be null or a function in React native

Viewed 808

I'm trying to testing my react-native app's components using jest and react testing utils.

However react-native-firebase is always returning "TypeError: Super expression must either be null or a function in React native". Even when I using they mocking approach.

I spend much time trying to figure out how to solve it, and trying another approaches I'm currently stuck in it and I don't have idea how to solve it.

Error details

ā— Test suite failed to run
TypeError: Super expression must either be null or a function

(node_modules/@babel/runtime/helpers/inherits.js:5:11)
node_modules/@react-native-firebase/app/lib/internal/RNFBNativeEventEmitter.js:25:26

<anonymous> (node_modules/@react-native-firebase/app/lib/internal/RNFBNativeEventEmitter.js:61:2)
(node_modules/@react-native-firebase/app/lib/internal/registry/nativeModule.js:21:1)

jest.config.js file

 module.exports = {
    preset: "react-native",
    setupFiles: ['./jest.setup.js'],
    transform: {},
     "transformIgnorePatterns": [
      "node_modules/(?!(@react-native|react-native|react-native-vector-icons|react-native-material-kit|@invertase|react-native-keyboard-aware-scrollview|react-native-easy-toast|react-native-image-picker|react-native-webview|react-native-background-upload|@react-native-firebase)/)"
    ],
    "setupFilesAfterEnv": [
      "@testing-library/jest-native/extend-expect"
    ]   
}        

jest.setup.js file


import * as ReactNative from 'react-native';

jest.doMock('react-native', () => {
  return Object.setPrototypeOf(
    {
      Platform: {
        OS: 'android',
        select: () => { },
      },
      NativeModules: {
        ...ReactNative.NativeModules,
        RNFBAppModule: {
          NATIVE_FIREBASE_APPS: [
            {
              appConfig: {
                name: '[DEFAULT]',
              },
              options: {},
            },

            {
              appConfig: {
                name: 'secondaryFromNative',
              },
              options: {},
            },
          ],
          addListener: jest.fn(),
          eventsAddListener: jest.fn(),
          eventsNotifyReady: jest.fn(),
        },
        RNFBAuthModule: {
          APP_LANGUAGE: {
            '[DEFAULT]': 'en-US',
          },
          APP_USER: {
            '[DEFAULT]': 'jestUser',
          },
          addAuthStateListener: jest.fn(),
          addIdTokenListener: jest.fn(),
          useEmulator: jest.fn(),
        },
        RNFBCrashlyticsModule: {},
        RNFBDatabaseModule: {
          on: jest.fn(),
          useEmulator: jest.fn(),
        },
        RNFBFirestoreModule: {
          settings: jest.fn(),
          documentSet: jest.fn(),
        },
        RNFBPerfModule: {},
        RNFBStorageModule: {
          useEmulator: jest.fn(),
        },
      },
    },
    ReactNative,
  );
}); 

any ideas?

1 Answers

I had the same problem, my solution was

jest.doMock('react-native', () => {
  return Object.setPrototypeOf(
    {
      Platform: {
        OS: 'android',
        select: () => {},
      },
      NativeModules: {
        ...ReactNative.NativeModules,
        RNFBAnalyticsModule: {
          logEvent: jest.fn(),
        },
        RNFBAppModule: {
          NATIVE_FIREBASE_APPS: [
            {
              appConfig: {
                name: '[DEFAULT]',
              },
              options: {},
            },

            {
              appConfig: {
                name: 'secondaryFromNative',
              },
              options: {},
            },
          ],
          addListener: jest.fn(),
          eventsAddListener: jest.fn(),
          eventsNotifyReady: jest.fn(),
          removeListeners: jest.fn(),
        },
        RNFBAuthModule: {
          APP_LANGUAGE: {
            '[DEFAULT]': 'en-US',
          },
          APP_USER: {
            '[DEFAULT]': 'jestUser',
          },
          addAuthStateListener: jest.fn(),
          addIdTokenListener: jest.fn(),
          useEmulator: jest.fn(),
        },
        RNFBMessagingModule: {
          onMessage: jest.fn(),
        },
      },
    },
    ReactNative,
  );
});

jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter')
Related