Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

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

Open
ghasemikasra39 opened this issue Sep 2, 2020 · 1 comment

Comments

@ghasemikasra39
Copy link

ghasemikasra39 commented Sep 2, 2020

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",
@zzau13
Copy link

zzau13 commented Jan 3, 2021

Something like this:

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants