首页 > 解决方案 > TypeError:超级表达式必须为 null 或 React Native 中的函数

问题描述

我正在尝试使用 jest 和 react 测试工具来测试我的 react-native 应用程序的组件。

但是 react-native-firebase 总是返回“TypeError: Super expression must be null or a function in React native”。即使我使用他们嘲笑的方法。

我花了很多时间试图弄清楚如何解决它,并尝试另一种我目前陷入困境的方法,我不知道如何解决它。

错误详情

● 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 文件

 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 文件


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,
  );
}); 

有任何想法吗?

标签: javascriptreact-nativetestingjestjsreact-native-firebase

解决方案


我有同样的问题,我的解决方案是

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

推荐阅读