我已经设置了几乎默认配置的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或其他东西添加到全局变量中以进行测试,但是我应该为所有测试执行此操作,还是有任何方法可以为所有测试创建一次?
似乎对于这种情况我们需要嘲笑reactotron-react-native
包。因此,要解决它,只需将“mocks”文件夹放入根文件夹,然后添加与包名称相同的文件。所以对于这种情况,它将是'reactotron-react-native.js'。该文件应该模拟包中的所有函数。对我来说帮助下一个:
const reactotron = {
configure: () => reactotron,
useReactNative: () => reactotron,
use: () => reactotron,
connect: () => reactotron,
};
module.exports = reactotron;
在这种情况下,它允许链接函数,因此可以更改测试中的调用顺序。