Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Push breaking jest tests for react-native

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 />,
  );
});
like image 974
Assem Avatar asked Mar 27 '17 11:03

Assem


2 Answers

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);
like image 90
luizParreira Avatar answered Sep 29 '22 06:09

luizParreira


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;
});
like image 30
Ruben Avatar answered Sep 29 '22 04:09

Ruben