简体   繁体   English

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

[英]How to resolve a NativeModule.RNLocalize is null error in test?

I'm using the package react-native-localize to provide localization in an app.我正在使用 package react-native-localize在应用程序中提供本地化。 I've already linked the library and it works fine running on a device.我已经链接了库,它在设备上运行良好。

Issue:问题:

When I test a component that imports react-native-localize .当我测试导入react-native-localize的组件时。 I get the error react-native-localize: NativeModule.RNLocalize is null .我收到错误react-native-localize: NativeModule.RNLocalize is null In order to resolve this null error I call jest.mock('react-native-localize');为了解决这个 null 错误,我调用jest.mock('react-native-localize'); at the top of the test file.在测试文件的顶部。 But I still get an error pointing to NativeModule.RNLocalize is null .但我仍然收到指向NativeModule.RNLocalize is null的错误。 I've also provided the mock as mentioned in the package README to no avail.我还提供了package 自述文件中提到的模拟,但无济于事。

import 'react-native';
import React from 'react';
import App from '../App';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
import * as RNLocalize from 'react-native-localize';

 // mocking the module here with jest.mock
jest.mock('react-native-localize');

it('renders correctly', () => {
  renderer.create(<App />);
});

Question:问题:

How can you resolve a NativeModule.RNLocalize is null error in test?如何解决测试中的 NativeModule.RNLocalize is null 错误?

Test Stack Trace:测试堆栈跟踪:

    FAIL  __tests__/App-test.js
  ● Test suite failed to run

    react-native-localize: NativeModule.RNLocalize is null. To fix this issue try these steps:
    • Run `react-native link react-native-localize` in the project root.
    • Rebuild and re-run the app.
    • If you are using CocoaPods on iOS, run `pod install` in the `ios` directory and then rebuild and re-run the app. You may also need to re-open Xcode to get the new pods.
    • Check that the library was linked correctly when you used the link command by running through the manual installation instructions in the README.
    * If you are getting this error while unit testing you need to mock the native module. Follow the guide in the README.
    If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-community/react-native-localize 

      16 | 
      17 | import {NavigationContainer} from '@react-navigation/native';
    > 18 | import * as RNLocalize from 'react-native-localize';
         | ^
      19 | import {Icon} from 'native-base';
      20 | import {createStackNavigator} from '@react-navigation/stack';
      21 | const Stack = createStackNavigator();

      at Object.<anonymous> (node_modules/react-native-localize/lib/commonjs/module.js:17:9)
      at Object.<anonymous> (node_modules/react-native-localize/lib/commonjs/index.js:3:1)
      at Object.<anonymous> (src/modules/AppView.js:18:1)
      at Object.<anonymous> (src/modules/AppViewContainer.js:2:1)
      at Object.<anonymous> (App.js:23:1)
      at Object.<anonymous> (__tests__/App-test.js:7:1)

I fixed this issue in my tests by adding these lines in my jest configuration file通过在我的 jest 配置文件中添加这些行,我在测试中解决了这个问题

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

I found the solution:- paste this code in your jest configuration file (setup.js)我找到了解决方案:- 将这段代码粘贴到你的 jest 配置文件 (setup.js)

jest.mock("react-native-localize", () => {
    getLocales: jest.fn(),   // use getLocales if you have used this, else use the one that you have used
    // you can add other functions mock here that you are using
  };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 NativeModule.RNLocalize 为 null? - How to fix NativeModule.RNLocalize is null? 错误错误:[@RNC/AsyncStorage]:NativeModule:AsyncStorage 是 null - ERROR Error: [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null 反应本机 typescript 屏幕测试返回测试套件运行失败。 错误反应本机权限:NativeModule.RNPermissions 是 null - react native typescript screen test returning Test suite failed to run. Error react-native-permissions: NativeModule.RNPermissions is null 在ReactNative中设置RevenueCat购买会抛出“ NativeModule”不能为空错误 - Setting up RevenueCat Purchases in ReactNative throws “NativeModule” cannot be null error NativeModule:AsyncStorage 是 null 和 Expo - NativeModule: AsyncStorage is null with Expo NativeModule RNCGeolocation 为空 - NativeModule RNCGeolocation is null 适用于 iOS 的 Swift NativeModule 始终为空 - Swift NativeModule for iOS is always null NativeModule:AsyncStorage 是 null,带有@RNC/AsyncStorage - NativeModule: AsyncStorage is null, with @RNC/AsyncStorage 安装 pod 后 Nativemodule AsyncStorage 为空 - Nativemodule AsyncStorage is null after pod install 为什么我会收到错误react-native-image-picker:NativeModule.ImagePickerManager为null - why will i get error react-native-image-picker : NativeModule.ImagePickerManager is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM