The Wayback Machine - https://web.archive.org/web/20200708163729/https://github.com/infinitered/reactotron/issues/1120
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactotron becomes undefined when mocking with jest #1120

Open
hannojg opened this issue Aug 9, 2019 · 5 comments
Open

Reactotron becomes undefined when mocking with jest #1120

hannojg opened this issue Aug 9, 2019 · 5 comments

Comments

@hannojg
Copy link

@hannojg hannojg commented Aug 9, 2019

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! :)

@rmevans9
Copy link
Collaborator

@rmevans9 rmevans9 commented Aug 28, 2019

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

@ac5m4ck
Copy link

@ac5m4ck ac5m4ck commented Jan 24, 2020

i'm having the same trouble here. :/

@BoKKeR
Copy link

@BoKKeR BoKKeR commented Feb 18, 2020

Same issue, any workaround?

@pedrolobato
Copy link

@pedrolobato pedrolobato commented Feb 27, 2020

Try:

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

jest.mock("reactotron-react-native", () => reactotron);
@bvodola
Copy link

@bvodola bvodola commented Apr 6, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.