最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Code Push breaking jest tests for react-native - Stack Overflow

programmeradmin6浏览0评论

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

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 />,
  );
});
Share Improve this question edited Mar 27, 2017 at 14:46 Andreas Köberle 111k58 gold badges280 silver badges307 bronze badges asked Mar 27, 2017 at 11:17 AssemAssem 12.1k5 gold badges62 silver badges102 bronze badges 7
  • 1 You need to create a mock for CodePush – Xeijp Commented Mar 28, 2017 at 1:24
  • codePush varibale is not defined or not a json' – RITESH ARORA Commented Apr 8, 2017 at 10:17
  • @Assem Chelli did you resolve your issues? Running into something similar myself... – Tom Hall Commented Jun 1, 2017 at 7:35
  • @TomHall not yet – Assem Commented Jun 2, 2017 at 22:53
  • Have you figured this out? I'm also running into this. – liver Commented Aug 3, 2017 at 20:28
 |  Show 2 more ments

4 Answers 4

Reset to default 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);

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;
});

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

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

发布评论

评论列表(0)

  1. 暂无评论