作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用包 react-native-localize
在应用程序中提供本地化。我已经链接了该库,它可以在设备上正常运行。
问题:
当我测试导入 的组件时 react 本地化 .我收到错误 react-native-localize: NativeModule.RNLocalize is null
.
为了解决这个空错误,我调用 jest.mock('react-native-localize');
在测试文件的顶部。但我仍然收到指向 NativeModule.RNLocalize is null
的错误消息.我还提供了在 package README 中提到的模拟。无济于事。
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 />);
});
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
};
});
关于react-native - 如何解决测试中的 NativeModule.RNLocalize is null 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61045191/
我正在使用包 react-native-localize在应用程序中提供本地化。我已经链接了该库,它可以在设备上正常运行。 问题: 当我测试导入 的组件时 react 本地化 .我收到错误 react
我是一名优秀的程序员,十分优秀!