Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test suite failed to run TypeError: Cannot read property 'default' of undefined

I'm trying to setup Jest on my react-native project but it hasn't been playing nice with bugsnag-react-native.

With my current test configuration I'm seeing errors related to bugsnag's leaveBreadcrumb function seen below:

 FAIL  app/__tests__/NetworkReducer.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'default' of undefined

      at Object.<anonymous> (app/__tests__/NetworkReducer.test.js:10:20)
          at Generator.next (<anonymous>)
          at Promise (<anonymous>)

I have a helper file that instantiates bugsnag:

helpers/bugSnag.js


//-------------------------------------------------------------------------------------------------
// Create a single instance of the bugsnag client so we don't have to duplicate our configuration
// anywhere.
//-------------------------------------------------------------------------------------------------
// https://docs.bugsnag.com/platforms/react-native/#basic-configuration

import { Client, Configuration } from 'bugsnag-react-native';
const config = new Configuration();
config.consoleBreadcrumbsEnabled = true;
config.notifyReleaseStages = ['testflight', 'production'];

const bugSnag = new Client(config);


export default bugSnag;

So in all my files I'm importing bugSnag from this helper file rather than declaring a new Client in each file, notably in my NetworkReducer.js where bugSnag.leaveBreadcrumb('someData') is causing me issues.

In my NetworkReducer.test.js I'm calling a mock:

 jest.mock(bugSnag, () => {
    return {
        leaveBreadcrumb: jest.fn()
    };
 });

where I'm also importing bugSnag from path/to/helpers/bugSnag

If I comment out the mock, I get a different error message on each of my reducer types that have a bugSnag.leaveBreadcrumb('someData') as seen below:

TypeError: _bugSnag2.default.leaveBreadcrumb is not a function

  at Object.network_prop_update (app/reducers/NetworkReducer.js:29:19)
  at app/reducers/createReducer.js:4:29
  at Object.<anonymous> (app/__tests__/NetworkReducer.test.js:80:29)
  at tryCallTwo (node_modules/promise/lib/core.js:45:5)
  at doResolve (node_modules/promise/lib/core.js:200:13)
  at new Promise (node_modules/promise/lib/core.js:66:3)

I thought I had a handle on this jest thing, and mocking, but I guess I've been proven wrong. I've attached my Jest's setup.js for extra reference:

    jest.mock('Linking', () => {
    return {
        addEventListener: jest.fn(),
        removeEventListener: jest.fn(),
        openURL: jest.fn(),
        canOpenURL: jest.fn(),
        getInitialURL: jest.fn(),
    };
});


jest.mock('PushNotificationIOS', () => {
    return {
        addEventListener: jest.fn(),
        requestPermissions: jest.fn(() => Promise.resolve()),
        getInitialNotification: jest.fn(() => Promise.resolve()),
    };
});


jest.mock('react-native-intercom', () => {
    return {
        registerIdentifiedUser: jest.genMockFn().mockReturnValue(Promise.resolve()),
        registerUnidentifiedUser: jest.genMockFn().mockReturnValue(Promise.resolve()),
        updateUser: jest.genMockFn().mockReturnValue(Promise.resolve()),
        reset: jest.genMockFn().mockReturnValue(Promise.resolve()),
        logEvent: jest.genMockFn().mockReturnValue(Promise.resolve()),
        handlePushMessage: jest.genMockFn().mockReturnValue(Promise.resolve()),
        displayMessenger: jest.genMockFn().mockReturnValue(Promise.resolve()),
        hideMessenger: jest.genMockFn().mockReturnValue(Promise.resolve()),
        displayMessageComposer: jest.genMockFn().mockReturnValue(Promise.resolve()),
        displayMessageComposerWithInitialMessage: jest.genMockFn().mockReturnValue(Promise.resolve()),
        displayConversationsList: jest.genMockFn().mockReturnValue(Promise.resolve()),
        getUnreadConversationCount: jest.genMockFn().mockReturnValue(Promise.resolve()),
        setLauncherVisibility: jest.genMockFn().mockReturnValue(Promise.resolve()),
        setInAppMessageVisibility: jest.genMockFn().mockReturnValue(Promise.resolve()),
        setupAPN: jest.genMockFn().mockReturnValue(Promise.resolve()),
        registerForPush: jest.genMockFn().mockReturnValue(Promise.resolve()),
        setUserHash: jest.genMockFn().mockReturnValue(Promise.resolve()),
        setBottomPadding: jest.genMockFn().mockReturnValue(Promise.resolve()),
        addEventListener: jest.fn(),
        removeEventListener: jest.fn()
    };
});


jest.mock('bugsnag-react-native', () => {
    return {
        leaveBreadcrumb: jest.fn(),
        Configuration: jest.fn(),
        Client: jest.fn()
    };
});
like image 702
MattyK14 Avatar asked Jan 04 '18 19:01

MattyK14


2 Answers

This was already answered, but in my case I had a different problem. Apparently react-test-renderer fails to instantiate Components if they are missing their constructor.

So I needed to add them following line to my PureComponent:

constructor(props) {
    super(props)
}
like image 147
Samer Murad Avatar answered Oct 10 '22 07:10

Samer Murad


My solution was to add the following mock instead:

jest.mock('../app/helpers/bugSnag', () => {
    return {
        leaveBreadcrumb: jest.fn(),
    };
});

A clear explanation to all this would be helpful.

like image 8
MattyK14 Avatar answered Oct 10 '22 07:10

MattyK14