簡體   English   中英

如何解決測試中的 NativeModule.RNLocalize is null 錯誤?

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

我正在使用 package react-native-localize在應用程序中提供本地化。 我已經鏈接了庫,它在設備上運行良好。

問題:

當我測試導入react-native-localize的組件時。 我收到錯誤react-native-localize: NativeModule.RNLocalize is null 為了解決這個 null 錯誤,我調用jest.mock('react-native-localize'); 在測試文件的頂部。 但我仍然收到指向NativeModule.RNLocalize is null的錯誤。 我還提供了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 />);
});

問題:

如何解決測試中的 NativeModule.RNLocalize is null 錯誤?

測試堆棧跟蹤:

    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)

通過在我的 jest 配置文件中添加這些行,我在測試中解決了這個問題

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

我找到了解決方案:- 將這段代碼粘貼到你的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM