javascript - 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,
);
});
有任何想法吗?
解决方案
我有同样的问题,我的解决方案是
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')
推荐阅读
- postgresql - '星期一'的时间段的postgresql列表| 09:00:00 | 11:00:00
- python - Python:装饰器中的 Lambda 函数详细信息
- excel - 选择特定选项按钮时选项按钮变灰
- c# - 如果指针结束的对象被停用,为什么不调用 OnPointerExit
- c++ - 代码编译没有任何输出,启动程序
- firebase - 这些模型中的哪一个是 Cloud Firestore 中更好的方法?
- mongoose - 无法使用 JWT 在博客文章 MERN 堆栈中填充作者
- php - PHP将字符串拆分为整数、字符串和特殊字符
- postgresql - 按月和按年的第一个记录
- ios - 有没有办法让 svg 图标适应苹果系统字体?