Reactotron: Error running tests on React Native

Created on 15 Aug 2019  路  6Comments  路  Source: infinitered/reactotron

Reproduce:

  • Create a simple project with react-native init (RN 0.60)
  • Set up Reactotron (exactly as described here)
  • Run _yarn test_
  • Tests fail:

Test suite failed to run

ReferenceError: WebSocket is not defined

  5 |     Reactotron.configure({ name: 'Wanderlust App' })
  6 |         .useReactNative({})
> 7 |         .connect();
    |          ^
  8 | }
  9 |

  at createSocket (node_modules/reactotron-react-native/dist/index.js:1:14125)
  at a.value (node_modules/reactotron-core-client/dist/index.js:1:9009)
  at Object.connect (src/config/ReactotronConfig.ts:7:10)
  at Object.<anonymous> (App.tsx:29:1)

  console.error node_modules/fbjs/lib/warning.js:30

You can see the files:
ReactotronConfig.ts file here
App.ts file here
App-tests.ts file here

Most helpful comment

@mthuong

__mocks__/globalMock.js:

jest.mock('global', () => ({
  ...global,
  WebSocket: function WebSocket() {},
}));

and configure your jest

` "setupFilesAfterEnv": [ "<rootDir>/__mocks__/globalMock.js" ]

All 6 comments

@rmevans9 I believe you fixed this a while back... Are you aware of this?

@NelsonPRSousa Nope, this is not something I am familiar with. I would suspect that all that is happening is reactotron-react-native expects that WebSocket is something that is available (which is a safe assumption in react-native) but it is unavailable in a test environment. I would say the quickest path to success here is to provide a mock implementation of WebSocket

Thanks

@NelsonPRSousa Do you have the sample WebSocket mock implementation?

No @mthuong, we are no longer using reactotron

@mthuong

__mocks__/globalMock.js:

jest.mock('global', () => ({
  ...global,
  WebSocket: function WebSocket() {},
}));

and configure your jest

` "setupFilesAfterEnv": [ "<rootDir>/__mocks__/globalMock.js" ]

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ashoat picture Ashoat  路  4Comments

sylar picture sylar  路  4Comments

scally picture scally  路  5Comments

felipemillhouse picture felipemillhouse  路  4Comments

wilr picture wilr  路  3Comments