Reactotron: Reactotron becomes undefined when mocking with jest

Created on 9 Aug 2019  路  5Comments  路  Source: infinitered/reactotron

I have issues to get Reactotron working in my jest react-native test environment.
Before running a test I simply do
jest.mock("reactotron-react-native");, but then I receive an error stating
TypeError: Cannot read property 'useReactNative' of undefined
Screenshot 2019-08-09 at 18 01 26

Unfortunately, I don't find any recommendation in your docs for setting reactotron up for testing.

Really appreciate your help in advance! :)

docs help wanted

Most helpful comment

We probably should do some good docs here. I will see if I can get something together soon.

All 5 comments

We probably should do some good docs here. I will see if I can get something together soon.

i'm having the same issue here. :/

Same issue, any workaround?

Try:

const reactotron = {
  configure: () => reactotron,
  useReactNative: () => reactotron,
  use: () => reactotron,
  connect: () => reactotron,
  clear: () => reactotron,
  createEnhancer: () => reactotron
};

jest.mock("reactotron-react-native", () => reactotron);

What worked for me:

// ReactotronConfig.js
import Reactotron, { asyncStorage } from 'reactotron-react-native';
import { AsyncStorage } from 'react-native';
import { reactotronRedux } from 'reactotron-redux';

const IP_ADDRESS = '192.168.15.88';

const reactotron = Reactotron.setAsyncStorageHandler(AsyncStorage)
  .configure({ host: IP_ADDRESS })
  .useReactNative({})
  .use(asyncStorage())
  .use(reactotronRedux())
  .connect();

export default reactotron;
//__mocks__/reactotron-react-native.js

/* eslint-disable no-undef */

export default {
  setAsyncStorageHandler: () => ({
    configure: () => ({
      useReactNative: () => ({
        use: () => ({
          use: () => ({
            connect: () => ({
              createEnhancer: jest.fn(),
            }),
          }),
        }),
      }),
    }),
  }),
};

export const asyncStorage = jest.fn();

Important: I had to mock the reactotron object in the same order I was calling in ReactotronConfig.js.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrewvy picture andrewvy  路  4Comments

Ashoat picture Ashoat  路  4Comments

ferminmoli picture ferminmoli  路  4Comments

Eyesonly88 picture Eyesonly88  路  4Comments

AdrienLemaire picture AdrienLemaire  路  3Comments