admin管理员组文章数量:1279207
When using the module react-native-push-notification
, I had this error:
FAIL __tests__/index.android.js
● Test suite failed to run
Invariant Violation: Native module cannot be null.
at invariant (node_modules/fbjs/lib/invariant.js:44:15)
at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:32:1)
at Object.<anonymous> (node_modules/react-native/Libraries/PushNotificationIOS/PushNotificationIOS.js:18:29)
at Object.get PushNotificationIOS [as PushNotificationIOS] (node_modules/react-native/Libraries/react-native/react-native.js:97:34)
at Object.<anonymous> (node_modules/react-native-push-notification/ponent/index.ios.js:10:23)
I tried to mock the module by creating __mocks__/react-native.js
and putting this code within it:
const rn = require('react-native')
jest.mock('PushNotificationIOS', () => ({
addEventListener: jest.fn(),
requestPermissions: jest.fn(),
then: jest.fn()
}));
module.exports = rn
Now, I have this error:
FAIL __tests__/index.android.js
● Test suite failed to run
TypeError: Cannot read property 'then' of null
at Object.<anonymous>.Notifications.popInitialNotification (node_modules/react-native-push-notification/index.js:278:42)
at Object.<anonymous>.Notifications.configure (node_modules/react-native-push-notification/index.js:93:6)
at Object.<anonymous> (app/utils/localPushNotification.js:4:39)
at Object.<anonymous> (app/actions/trip.js:5:28)
How I could mock fully this module the right way?
When using the module react-native-push-notification
, I had this error:
FAIL __tests__/index.android.js
● Test suite failed to run
Invariant Violation: Native module cannot be null.
at invariant (node_modules/fbjs/lib/invariant.js:44:15)
at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:32:1)
at Object.<anonymous> (node_modules/react-native/Libraries/PushNotificationIOS/PushNotificationIOS.js:18:29)
at Object.get PushNotificationIOS [as PushNotificationIOS] (node_modules/react-native/Libraries/react-native/react-native.js:97:34)
at Object.<anonymous> (node_modules/react-native-push-notification/ponent/index.ios.js:10:23)
I tried to mock the module by creating __mocks__/react-native.js
and putting this code within it:
const rn = require('react-native')
jest.mock('PushNotificationIOS', () => ({
addEventListener: jest.fn(),
requestPermissions: jest.fn(),
then: jest.fn()
}));
module.exports = rn
Now, I have this error:
FAIL __tests__/index.android.js
● Test suite failed to run
TypeError: Cannot read property 'then' of null
at Object.<anonymous>.Notifications.popInitialNotification (node_modules/react-native-push-notification/index.js:278:42)
at Object.<anonymous>.Notifications.configure (node_modules/react-native-push-notification/index.js:93:6)
at Object.<anonymous> (app/utils/localPushNotification.js:4:39)
at Object.<anonymous> (app/actions/trip.js:5:28)
How I could mock fully this module the right way?
Share Improve this question asked Apr 1, 2017 at 14:17 AssemAssem 12.1k5 gold badges62 silver badges102 bronze badges 2-
Why you don't mock
react-native-push-notification
. – Andreas Köberle Commented Apr 1, 2017 at 21:05 - @AndreasKöberle I was looking for the way to mocking it, I already did. – Assem Commented Apr 1, 2017 at 21:08
3 Answers
Reset to default 8I mocked the module PushNotificationIOS
by creating a setup file jest/setup.js
:
jest.mock('PushNotificationIOS', () => {
return {
addEventListener: jest.fn(),
requestPermissions: jest.fn(() => Promise.resolve()),
getInitialNotification: jest.fn(() => Promise.resolve()),
}
});
I've configured jest to run this setup file by adding this line into packages.json
:
"jest": {
...
"setupFiles": ["./jest/setup.js"],
}
The problem is that in the line where the error is thrown, the lib tries to call getInitialNotification
in the react-native module and expect an promise with some kind of result to be return. So you need to add this function to the mock and let it return a resolved promise.
You can directly put react-native-push-notification-ios
in tranformIgnorePatterns
.
本文标签: javascriptHow to mock Push notification native module in React native jest testsStack Overflow
版权声明:本文标题:javascript - How to mock Push notification native module in React native jest tests? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741222679a2361266.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论