react-native-mixpanel icon indicating copy to clipboard operation
react-native-mixpanel copied to clipboard

Testing with Jest and Enzyme: TypeError: Cannot read property 'sharedInstanceWithToken' of undefined

Open ghasemikasra39 opened this issue 5 years ago • 1 comments

When running my test using Jest and Enzyme, I get this error:

  ● Test suite failed to run

    TypeError: Cannot read property 'sharedInstanceWithToken' of undefined

      47 | class MixPanelClient {
      48 |   constructor() {
    > 49 |     Mixpanel.default.sharedInstanceWithToken(
         |                      ^
      50 |       this.getProjectToken(),
      51 |       false,
      52 |       true,

      at new MixPanelClient (src/services/utility/MixPanelClient.ts:49:22)
      at Object.<anonymous> (src/services/utility/MixPanelClient.ts:107:16)
      at Object.<anonymous> (__tests__/bootstrapTests.ts:1:1)

In my jest setup, the mocking did not solve this issue:

jest.mock('react-native-mixpanel', () => ({
  sharedInstanceWithToken: jest.fn(),
  trackWithProperties: jest.fn(),
}));

    "react-native": "0.62.2",
    "react-native-mixpanel": "^1.2.0",
    "enzyme": "^3.11.0",
    "jest": "^26.4.2",

ghasemikasra39 avatar Sep 02 '20 20:09 ghasemikasra39

Something like this:

jest.mock('react-native-mixpanel', () => ({
    __esModule: true,
    default: {
        sharedInstanceWithToken: jest.fn(),
        trackWithProperties: jest.fn(),
    }
}))

zzau13 avatar Jan 03 '21 05:01 zzau13