-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jest test failing on TypeError: _reactNativeOnesignal.default.init is not a function (OneSignal + Jest) #738
Comments
Hello, |
@rgomezp this is my NotificationService |
Hello, |
@AfanasievN OneSignal’s react native SDK is just a wrapper on top of a native SDK framework. Are your tests pure JS or do they emulate iOS/Android and load our native framework? I would suggest using mocking so that your tests don’t use OneSignal react native code at all. |
Hello, @Nightsd01 @rgomezp I found a solution today. My workaround is to add to my jest-setup file this code ---> Thanks for your time and awareness, we can close this issue |
I have TypeError: user.generateAuthToken is not a function problem and my problem is not solve |
I am using "jest": "^26.5.3", "supertest": "^5.0.0", |
This error has fix?
Test suite failed to run
|
Here is a full set of mock functions based on the OneSignal type definition. Because it is so large, I put it in a separate mock file, rather than use // __mocks__/react-native-onesignal.js
export default {
setAppId: jest.fn(),
addPermissionObserver: jest.fn(),
clearPermissionObservers: jest.fn(),
addSubscriptionObserver: jest.fn(),
clearSubscriptionObservers: jest.fn(),
addEmailSubscriptionObserver: jest.fn(),
clearEmailSubscriptionObservers: jest.fn(),
addSMSSubscriptionObserver: jest.fn(),
clearSMSSubscriptionObservers: jest.fn(),
setNotificationWillShowInForegroundHandler: jest.fn(),
setNotificationOpenedHandler: jest.fn(),
promptForPushNotificationsWithUserResponse: jest.fn(),
registerForProvisionalAuthorization: jest.fn(),
disablePush: jest.fn(),
unsubscribeWhenNotificationsAreDisabled: jest.fn(),
enterLiveActivity: jest.fn(),
exitLiveActivity: jest.fn(),
isLocationShared: jest.fn(),
setLocationShared: jest.fn(),
promptLocation: jest.fn(),
getDeviceState: jest.fn(),
setLanguage: jest.fn(),
sendTag: jest.fn(),
sendTags: jest.fn(),
getTags: jest.fn(),
deleteTag: jest.fn(),
deleteTags: jest.fn(),
setEmail: jest.fn(),
logoutEmail: jest.fn(),
setSMSNumber: jest.fn(),
logoutSMSNumber: jest.fn(),
postNotification: jest.fn(),
clearOneSignalNotifications: jest.fn(),
removeNotification: jest.fn(),
removeGroupedNotifications: jest.fn(),
setLaunchURLsInApp: jest.fn(),
setExternalUserId: jest.fn(),
removeExternalUserId: jest.fn(),
setInAppMessageClickHandler: jest.fn(),
setInAppMessageLifecycleHandler: jest.fn(),
addTrigger: jest.fn(),
addTriggers: jest.fn(),
removeTriggerForKey: jest.fn(),
removeTriggersForKeys: jest.fn(),
getTriggerValueForKey: jest.fn(),
pauseInAppMessages: jest.fn(),
sendOutcome: jest.fn(),
sendUniqueOutcome: jest.fn(),
sendOutcomeWithValue: jest.fn(),
userProvidedPrivacyConsent: jest.fn(),
requiresUserPrivacyConsent: jest.fn(),
setRequiresUserPrivacyConsent: jest.fn(),
provideUserConsent: jest.fn(),
setLogLevel: jest.fn(),
clearHandlers: jest.fn(),
} |
Here's a new mock for v5.x module.exports = {
OneSignal: {
initialize: jest.fn(),
login: jest.fn(),
logout: jest.fn(),
setConsentRequired: jest.fn(),
setConsentGiven: jest.fn(),
Debug: {
setLogLevel: jest.fn(),
setAlertLevel: jest.fn(),
},
LiveActivities: {
enter: jest.fn(),
exit: jest.fn(),
setPushToStartToken: jest.fn(),
removePushToStartToken: jest.fn(),
setupDefault: jest.fn(),
startDefault: jest.fn(),
},
User: {
pushSubscription: {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
getPushSubscriptionId: jest.fn(),
getIdAsync: jest.fn().mockResolvedValue(null),
getPushSubscriptionToken: jest.fn(),
getTokenAsync: jest.fn().mockResolvedValue(null),
getOptedIn: jest.fn(),
getOptedInAsync: jest.fn().mockResolvedValue(false),
optOut: jest.fn(),
optIn: jest.fn(),
},
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
getOnesignalId: jest.fn().mockResolvedValue(null),
getExternalId: jest.fn().mockResolvedValue(null),
setLanguage: jest.fn(),
addAlias: jest.fn(),
addAliases: jest.fn(),
removeAlias: jest.fn(),
removeAliases: jest.fn(),
addEmail: jest.fn(),
removeEmail: jest.fn(),
addSms: jest.fn(),
removeSms: jest.fn(),
addTag: jest.fn(),
addTags: jest.fn(),
removeTag: jest.fn(),
removeTags: jest.fn(),
getTags: jest.fn().mockResolvedValue({}),
},
Notifications: {
hasPermission: jest.fn(),
getPermissionAsync: jest.fn().mockResolvedValue(false),
requestPermission: jest.fn().mockResolvedValue(false),
canRequestPermission: jest.fn().mockResolvedValue(false),
registerForProvisionalAuthorization: jest.fn(),
permissionNative: jest.fn().mockResolvedValue(null),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
clearAll: jest.fn(),
removeNotification: jest.fn(),
removeGroupedNotifications: jest.fn(),
},
InAppMessages: {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
addTrigger: jest.fn(),
addTriggers: jest.fn(),
removeTrigger: jest.fn(),
removeTriggers: jest.fn(),
clearTriggers: jest.fn(),
setPaused: jest.fn(),
getPaused: jest.fn().mockResolvedValue(false),
},
Location: {
requestPermission: jest.fn(),
setShared: jest.fn(),
isShared: jest.fn().mockResolvedValue(false),
},
Session: {
addOutcome: jest.fn(),
addUniqueOutcome: jest.fn(),
addOutcomeWithValue: jest.fn(),
},
},
} |
Description:
Jest test failing on TypeError: _reactNativeOnesignal.default.init is not a function
I am using jest tests and I've implemented oneSignal in my App.js
and when I try to run yarn test it then not passing tests.
Here is Error
My App.js file:
My App.test.js:
My jest-setup.js:
Environment
"react": "16.8.4",
"react-native": "0.59.1",
"babel-jest": "24.5.0",
"jest": "24.5.0",
"react-test-renderer": "16.8.4"
"jest": {
"preset": "react-native",
"transform": {
"^.+\.js$": "/node_modules/react-native/jest/preprocessor.js"
},
"transformIgnorePatterns": [
"./node_modules/(?!(react-native|styled-components|react-navigation|react-test-renderer|react-native-onesignal|react-native-localization|react-native-gesture-handler|react-native-vector-icons|@react-native-community/async-storage|@react-native-community/netinfo|@react-navigation/.*))"
],
"setupFiles": [
"./jest-setup.js"
],
"moduleNameMapper": {
"^styled-components$": "/node_modules/styled-components/native/dist/styled-components.native.cjs.js"
}
}
The text was updated successfully, but these errors were encountered: