Skip to content
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

Closed
AfanasievN opened this issue Mar 21, 2019 · 11 comments

Comments

@AfanasievN
Copy link

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
Снимок экрана 2019-03-21 в 11 49 42

My App.js file:
Снимок экрана 2019-03-21 в 11 50 03

My App.test.js:
Снимок экрана 2019-03-21 в 11 49 53

My jest-setup.js:
Снимок экрана 2019-03-21 в 11 53 14

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"
}
}

@rgomezp rgomezp changed the title Jest test failing on TypeError: _reactNativeOnesignal.default.init is not a function Jest test failing on TypeError: _reactNativeOnesignal.default.init is not a function (OneSignal + Jest) Apr 1, 2019
@rgomezp
Copy link
Contributor

rgomezp commented Apr 1, 2019

Hello,
Thank you for your patience. Were you able to resolve? I'd be interested in what notificationsService does. It would be helpful if you shared a sample project so I could build it and take a closer look.

@AfanasievN
Copy link
Author

@rgomezp this is my NotificationService
Снимок экрана 2019-04-02 в 12 15 32

And this one is NotificationsHandlers
Снимок экрана 2019-04-02 в 12 15 53

@rgomezp
Copy link
Contributor

rgomezp commented Apr 16, 2019

Hello,
Could you please share a repro project with us through our support channel at onesignal.com so we can build it?

@Nightsd01
Copy link
Contributor

@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.

@AfanasievN
Copy link
Author

AfanasievN commented May 2, 2019

Hello, @Nightsd01 @rgomezp I found a solution today.
I still don't know why but jest could not figure out functions after dot like on screen shot
Снимок экрана 2019-05-02 в 16 49 12

My workaround is to add to my jest-setup file this code --->
jest.mock('react-native-onesignal', () => { return { OneSignal: jest.fn(() => 'undefined'), init: jest.fn(() => 'undefined'), inFocusDisplaying: jest.fn(() => 'undefined'), addEventListener: jest.fn(() => 'undefined'), }; });
Снимок экрана 2019-05-02 в 16 50 16

Thanks for your time and awareness, we can close this issue

Снимок экрана 2019-05-02 в 16 53 14

@rgomezp rgomezp closed this as completed May 2, 2019
@0sudiptoroy
Copy link

I have TypeError: user.generateAuthToken is not a function problem and my problem is not solve

@0sudiptoroy
Copy link

code
terminal error in jest

@0sudiptoroy
Copy link

I am using "jest": "^26.5.3", "supertest": "^5.0.0",

@rodrigogaraujo
Copy link

rodrigogaraujo commented Apr 23, 2022

This error has fix?

I put the mock but this error continue

Test suite failed to run

Invariant Violation: Native module cannot be null.
import OneSignal from 'react-native-onesignal'
`jest.mock('react-native-onesignal', () => { ....`

@blwinters
Copy link

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 jest.mock in the jest setup file.

// __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(),
}

@blwinters
Copy link

blwinters commented Nov 8, 2024

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(),
    },
  },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants