8

I have configured an app for code-push, it works well except for jest tests. It fails in rendering app for this error:

TypeError: Cannot read property 'CheckFrequency' of undefined

  at Object.<anonymous> (app/index.js:7:66)
  at Object.<anonymous> (index.ios.js:5:12)
  at Object.<anonymous> (__tests__/index.ios.js:4:12)

in this line:

const codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };

The test code is:

import App from '../index.ios';

it('renders correctly', () => {
  const tree = renderer.create(
      <App />,
  );
});
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
Assem
  • 11,574
  • 5
  • 59
  • 97

4 Answers4

11

I came across this problem while integrating codePush into the React Native app I am currently working on. What worked for me was:

  1. Creating a file __mocks__/react-native-code-push.js.

Adding the following code to it:

const codePush = {
  InstallMode: {ON_NEXT_RESTART: 'ON_APP_RESTART'},
  CheckFrequency: {ON_APP_RESUME: 'ON_APP_RESUME'}
};

const cb = _ => app => app;
Object.assign(cb, codePush);
export default cb;

On my index.js file, I have:

import codePush from 'react-native-code-push';
import MyApp from './src/'

const codePushOptions = {
  installMode: codePush.InstallMode.ON_NEXT_RESTART,
  checkFrequency: codePush.CheckFrequency.ON_APP_RESUME
};

export default codePush(codePushOptions)(MyApp);
luizParreira
  • 1,079
  • 12
  • 14
7

Similar to what Tom Hall describes, this mock does work for me:

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

    checkForUpdate: jest.fn(),
    codePushify: jest.fn(),
    getConfiguration: jest.fn(),
    getCurrentPackage: jest.fn(),
    getUpdateMetadata: jest.fn(),
    log: jest.fn(),
    notifyAppReady: jest.fn(),
    notifyApplicationReady: jest.fn(),
    sync: jest.fn(),
  });
  return cp;
});
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Ruben
  • 79
  • 1
  • 2
  • 3
    Why the currying? It does not pass like that. After modifying to `const cp = (app: any) => app;` it passes. – adambene Oct 30 '17 at 17:22
1

In your tests, underneath your import App from '../index.ios';, add the following:

jest.mock('react-native-code-push', () => {
    return jest.fn(() => ({
        InstallMode: jest.fn(),
        CheckFrequency: jest.fn(),
        CodePushComponent: jest.fn(),
        codePushify: jest.fn()
    }));
});
Tom Hall
  • 4,258
  • 2
  • 23
  • 23
  • I believe this will still result in the error `TypeError: Cannot read property 'MANUAL' of undefined` – Keith Jun 19 '17 at 18:26
0

You need a mock up for code-push to work, this line CodePush.CheckFrequency.MANUAL will always produce the null.

Assem
  • 11,574
  • 5
  • 59
  • 97
Remario
  • 3,813
  • 2
  • 18
  • 25