react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

How to setup jest test?

Open anhtuank7c opened this issue 9 years ago • 2 comments

Hi, I do setup jest test like this:

jest.mock('react-native-push-notification', () => ({
    onRegister: jest.fn(),
    onNotification: jest.fn(),
    addEventListener: jest.fn(),
    requestPermissions: jest.fn(),
}));

But when run npm test i got error:

TypeError: _reactNativePushNotification2.default.configure is not a function

So, i want to ask you correct way to setup jest test?

anhtuank7c avatar Jan 25 '17 04:01 anhtuank7c

The configure function is called when the library initialises, so it needs to be included

jest.mock('react-native-push-notification', () => ({
    configure: jest.fn(),
    onRegister: jest.fn(),
    onNotification: jest.fn(),
    addEventListener: jest.fn(),
    requestPermissions: jest.fn(),
}));

jonesn3 avatar Feb 28 '17 16:02 jonesn3

@JonesN7 I tried to mock 'react-native-push-notification' but I am still getting the error: ? 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-implementation.js:90:34)
  at Object.<anonymous> (node_modules/react-native-push-notification/component/index.ios.js:10:23)

I am using:

"react": "16.0.0-alpha.12",
"react-native": "0.45.1",
"react-native-push-notification": "^3.0.0",
"jest": "20.0.4",

and my jest configuration:

"jest": {
        "preset": "react-native",
        "testResultsProcessor": "jest-teamcity-reporter",
        "collectCoverageFrom": [
            "src/**/*.js"
        ],
        "coverageReporters": [
            "html",
            "text"
        ],
        "transformIgnorePatterns": [
            "node_modules/(?!(jest-)?react-native|react-navigation)"
        ],
        "setupFiles": [
            "<rootDir>/test/globalFetch.js",
            "<rootDir>/__mock__/react-native-i18n.js",
            "<rootDir>/__mock__/pushNotificationIOS.js"
        ]
    }

my mock file pushNotificationIOS.js is:

const PushNotificationIOSMock = jest.mock('react-native-push-notification', () => ({
    configure: jest.fn(),
    onRegister: jest.fn(),
    onNotification: jest.fn(),
    addEventListener: jest.fn(),
    requestPermissions: jest.fn(() => Promise.resolve()),
    getInitialNotification: jest.fn(() => Promise.resolve()),
}));

global.PushNotificationIOS = PushNotificationIOSMock;

solution I needed to change global variable from: global.PushNotificationIOS to: global.Notification which I use in my application.

b-asaf avatar Oct 09 '17 12:10 b-asaf