测试中如何解决NativeModule.RNLocalize为null错误?

时间:2020-04-05 15:44:42

标签: react-native jestjs native-module react-native-localize

我正在使用软件包import tensorflow.compat.v1 as tf tf.disable_v2_behavior()在应用程序中提供本地化。我已经链接了库,并且可以在设备上正常运行。

问题:

当我测试导入 react-native-localize 的组件时。我收到错误import tensorflow as tf。 为了解决此空错误,我在测试文件的顶部调用react-native-localize。但是我仍然遇到指向react-native-localize: NativeModule.RNLocalize is null的错误。我还提供了axios request中提到的模拟,但无济于事。

jest.mock('react-native-localize');

问题:

如何解决NativeModule.RNLocalize在测试中为空错误?

测试堆栈跟踪:

NativeModule.RNLocalize is null

1 个答案:

答案 0 :(得分:1)

我通过在Jest配置文件中添加以下行来修复测试中的问题

jest.mock("react-native-localize", () => {
  return {
    getLocales: jest.fn(),
    // you can add other functions mock here that you are using
  };
});