【问题标题】:TypeError: Super expression must either be null or a function in React nativeTypeError:超级表达式必须为 null 或 React Native 中的函数
【发布时间】:2021-10-20 12:32:03
【问题描述】:

我正在尝试使用 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,
  );
}); 

有什么想法吗?

【问题讨论】:

    标签: javascript react-native testing jestjs react-native-firebase


    【解决方案1】:

    我遇到了同样的问题,我的解决方案是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多