React-native-code-push: Jest tests fail with code push

Created on 4 Aug 2017  路  6Comments  路  Source: microsoft/react-native-code-push

Description

I installed code-push in my app (which uses wix's react-native-navigation) using a CodePushComponent to go into my app's home screen like so:

import React from 'react'
import codePush from 'react-native-code-push'

let codePushOptions = { installMode: codePush.InstallMode.ON_NEXT_RESUME, checkFrequency: codePush.CheckFrequency.ON_APP_RESUME }
codePush(codePushOptions)(CodePushComponent)

export class CodePushComponent extends React.Component {
  render () {
    return null
  }
}

module.exports = CodePushComponent

I wrote a simple jest test:

import 'react-native'
import React from 'react'
import Index from '../index.ios.js'

jest.mock('react-native-code-push', () => {
  return jest.fn(() => ({
      InstallMode: jest.fn(),
      CheckFrequency: jest.fn(),
  }))
})

import renderer from 'react-test-renderer'

it('renders correctly', () => {
  const tree = renderer.create(
    <Index />
  )
})

Expected: test suite to run without errors
What actually happens: I run npm test

FAIL  __tests__/index.ios.js
Test suite failed to run
TypeError: Cannot read property 'InstallMode' of undefined at Object.<anonymous> (src/components/Shared/CodePushComponent.js:4:63)

meaning this line: let codePushOptions = { installMode: codePush.InstallMode.ON_NEXT_RESUME, checkFrequency: codePush.CheckFrequency.ON_APP_RESUME } causes the error.

How do I mock code-push correctly? I've also tried ignoring the native module in package.json

"jest": {
    "preset": "react-native",
    "transformIgnorePatterns": [
        "node_modules/(?!react-native|react-native-code-push)"
    ]
},

but that doesn't seem to work. I also looked at this stackoverflow post but the OP hasn't figured it out yet.

I'm sure many of you use code-push with jest - how have you accomplished this?

Additional Information

"react": "16.0.0-alpha.12",
"react-native": "0.46.4",
"react-native-code-push": "^4.1.0-beta",
tests waiting-for-clarification

Most helpful comment

This stackoverflow post helped a lot but didn't fully solve the issue. Functions like getUpdateMetadata that should return promises failed.

Fixed like this:

jest.mock('react-native-code-push', () => {
  const cp = () => app => app;
  Object.assign(cp, {
    InstallMode: {},
    CheckFrequency: {},
    SyncStatus: {},
    UpdateState: {},
    DeploymentStatus: {},
    DEFAULT_UPDATE_DIALOG: {},

    allowRestart: jest.fn(),
    checkForUpdate: jest.fn(() => Promise.resolve(null)),
    disallowRestart: jest.fn(),
    getCurrentPackage: jest.fn(() => Promise.resolve(null)),
    getUpdateMetadata: jest.fn(() => Promise.resolve(null)),
    notifyAppReady: jest.fn(() => Promise.resolve()),
    restartApp: jest.fn(),
    sync: jest.fn(() => Promise.resolve(1)),
    clearUpdates: jest.fn(),
  });
  return cp;
});

Note that you can mock the constant values as well if you need them, eg: InstallMode: { IMMEDIATE: 'immediate' }

code-push-react-native: 6.1.0

All 6 comments

Hi @oliviachang29, thanks for reaching us and sorry for the delayed response. The exception you have

TypeError: Cannot read property 'InstallMode' of undefined at Object.<anonymous> (src/components/Shared/CodePushComponent.js:4:63)

frequently happens for users who want to integrate CodePush into the React Native app that uses react-native-navigation module. Please take a look at https://github.com/Microsoft/react-native-code-push/issues/790#issuecomment-294148452 and let me know if it helps.

@oliviachang29 please let us know if you have a chance to look at this.

Sorry, I don't have time to look at this right now.

@oliviachang29, np, that's ok. For that moment I'll close the issue, please reopen it if needed when you get a chance to verify.

I stumbled upon it this problem, what worked for me was:
https://stackoverflow.com/a/47412852/4320404

This stackoverflow post helped a lot but didn't fully solve the issue. Functions like getUpdateMetadata that should return promises failed.

Fixed like this:

jest.mock('react-native-code-push', () => {
  const cp = () => app => app;
  Object.assign(cp, {
    InstallMode: {},
    CheckFrequency: {},
    SyncStatus: {},
    UpdateState: {},
    DeploymentStatus: {},
    DEFAULT_UPDATE_DIALOG: {},

    allowRestart: jest.fn(),
    checkForUpdate: jest.fn(() => Promise.resolve(null)),
    disallowRestart: jest.fn(),
    getCurrentPackage: jest.fn(() => Promise.resolve(null)),
    getUpdateMetadata: jest.fn(() => Promise.resolve(null)),
    notifyAppReady: jest.fn(() => Promise.resolve()),
    restartApp: jest.fn(),
    sync: jest.fn(() => Promise.resolve(1)),
    clearUpdates: jest.fn(),
  });
  return cp;
});

Note that you can mock the constant values as well if you need them, eg: InstallMode: { IMMEDIATE: 'immediate' }

code-push-react-native: 6.1.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaysig picture jaysig  路  3Comments

DeDuckProject picture DeDuckProject  路  3Comments

kevando picture kevando  路  4Comments

djw27 picture djw27  路  3Comments

osdnk picture osdnk  路  3Comments