我正在尝试在我的react-native项目上设置Jest,但它与bugsnag-react-native的配合不好。
使用当前的测试配置,我看到与bugsnag的leaveBreadcrumb
函数相关的错误如下所示:
FAIL app/__tests__/NetworkReducer.test.js
● Test suite failed to run
TypeError: Cannot read property 'default' of undefined
at Object.<anonymous> (app/__tests__/NetworkReducer.test.js:10:20)
at Generator.next (<anonymous>)
at Promise (<anonymous>)
我有一个实例化bugsnag的帮助文件:
helpers/bugSnag.js
//-------------------------------------------------------------------------------------------------
// Create a single instance of the bugsnag client so we don't have to duplicate our configuration
// anywhere.
//-------------------------------------------------------------------------------------------------
// https://docs.bugsnag.com/platforms/react-native/#basic-configuration
import { Client, Configuration } from 'bugsnag-react-native';
const config = new Configuration();
config.consoleBreadcrumbsEnabled = true;
config.notifyReleaseStages = ['testflight', 'production'];
const bugSnag = new Client(config);
export default bugSnag;
因此,在我的所有文件中,我都是从此帮助程序文件中导入bugSnag,而不是在每个文件中声明一个新的Client,尤其是在
NetworkReducer.js
引起我问题的bugSnag.leaveBreadcrumb('someData')
中。在我的
NetworkReducer.test.js
中,我正在调用一个模拟: jest.mock(bugSnag, () => {
return {
leaveBreadcrumb: jest.fn()
};
});
我也在其中导入
bugSnag from path/to/helpers/bugSnag
如果我注释掉该模拟,则会在每种带有
bugSnag.leaveBreadcrumb('someData')
的reducer类型上收到不同的错误消息,如下所示:TypeError: _bugSnag2.default.leaveBreadcrumb is not a function
at Object.network_prop_update (app/reducers/NetworkReducer.js:29:19)
at app/reducers/createReducer.js:4:29
at Object.<anonymous> (app/__tests__/NetworkReducer.test.js:80:29)
at tryCallTwo (node_modules/promise/lib/core.js:45:5)
at doResolve (node_modules/promise/lib/core.js:200:13)
at new Promise (node_modules/promise/lib/core.js:66:3)
我以为我对这个 Jest 东西和 mock 有把握,但是我想我被证明是错误的。我已经附上了Jest的
setup.js
,以获取更多引用: jest.mock('Linking', () => {
return {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
openURL: jest.fn(),
canOpenURL: jest.fn(),
getInitialURL: jest.fn(),
};
});
jest.mock('PushNotificationIOS', () => {
return {
addEventListener: jest.fn(),
requestPermissions: jest.fn(() => Promise.resolve()),
getInitialNotification: jest.fn(() => Promise.resolve()),
};
});
jest.mock('react-native-intercom', () => {
return {
registerIdentifiedUser: jest.genMockFn().mockReturnValue(Promise.resolve()),
registerUnidentifiedUser: jest.genMockFn().mockReturnValue(Promise.resolve()),
updateUser: jest.genMockFn().mockReturnValue(Promise.resolve()),
reset: jest.genMockFn().mockReturnValue(Promise.resolve()),
logEvent: jest.genMockFn().mockReturnValue(Promise.resolve()),
handlePushMessage: jest.genMockFn().mockReturnValue(Promise.resolve()),
displayMessenger: jest.genMockFn().mockReturnValue(Promise.resolve()),
hideMessenger: jest.genMockFn().mockReturnValue(Promise.resolve()),
displayMessageComposer: jest.genMockFn().mockReturnValue(Promise.resolve()),
displayMessageComposerWithInitialMessage: jest.genMockFn().mockReturnValue(Promise.resolve()),
displayConversationsList: jest.genMockFn().mockReturnValue(Promise.resolve()),
getUnreadConversationCount: jest.genMockFn().mockReturnValue(Promise.resolve()),
setLauncherVisibility: jest.genMockFn().mockReturnValue(Promise.resolve()),
setInAppMessageVisibility: jest.genMockFn().mockReturnValue(Promise.resolve()),
setupAPN: jest.genMockFn().mockReturnValue(Promise.resolve()),
registerForPush: jest.genMockFn().mockReturnValue(Promise.resolve()),
setUserHash: jest.genMockFn().mockReturnValue(Promise.resolve()),
setBottomPadding: jest.genMockFn().mockReturnValue(Promise.resolve()),
addEventListener: jest.fn(),
removeEventListener: jest.fn()
};
});
jest.mock('bugsnag-react-native', () => {
return {
leaveBreadcrumb: jest.fn(),
Configuration: jest.fn(),
Client: jest.fn()
};
});
最佳答案
这已经得到了回答,但是就我而言,我有一个不同的问题。
显然,如果react-test-renderer
缺少组件的构造函数,则无法实例化它们。
所以我需要将它们添加到PureComponent
下面:
constructor(props) {
super(props)
}
关于javascript - 测试套件无法运行TypeError:无法读取未定义的属性 'default',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48102352/