嘲笑Reactotron开玩笑

时间:2019-03-07 07:48:56

标签: react-native jestjs reactotron

我已经使用几乎默认配置设置了Reactotron:

import Reactotron from 'reactotron-react-native';
import { reactotronRedux } from 'reactotron-redux';

const reactotron = Reactotron
  .configure()
  .useReactNative()
  .use(reactotronRedux())
  .connect();

export default reactotron;

并将其导入'index.js':

if (__DEV__) { import('./app/utils/reactotron'); }

但是在此之后,大多数Jest测试均失败,并出现下一个错误:

ReferenceError: WebSocket is not defined

   8 |   .useReactNative()
   9 |   .use(reactotronRedux())
> 10 |   .connect();
     | ^
  11 | 
  12 | export default reactotron;

  at createSocket (node_modules/reactotron-react-native/dist/index.js:1:13571)
  at a.value (node_modules/reactotron-core-client/dist/index.js:1:8397)
  at Object.<anonymous> (app/utils/reactotron.js:10:1)

看起来我需要在全局变量中添加WebSocket或其他内容以进行测试,但是我应该在所有测试中都使用它,还是有办法在所有测试中使其一次使用?

1 个答案:

答案 0 :(得分:1)

看来,在这种情况下,我们需要模拟reactotron-react-native软件包。因此,要解决该问题,只需在根文件夹中放入一个 mocks 文件夹,然后在其中添加与包同名的文件。因此,在这种情况下,它将是“ reactotron-react-native.js”。该文件应模拟软件包中的所有功能。对我来说,帮助下一个:

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

module.exports = reactotron;

在这种情况下,它允许链接函数,因此可以更改测试中的调用顺序。