【问题标题】:Unit Testing React Native: NativeModules.RNViewShot is undefined. Make sure the library is linked on the native side单元测试 React Native:NativeModules.RNViewShot 未定义。确保库在本机端链接
【发布时间】:2018-12-24 02:20:54
【问题描述】:

当我对应用程序中的各种组件运行一些单元测试时,我不断收到这个烦人的警告:

NativeModules.RNViewShot is undefined. Make sure the library is linked on the native side.

在我的根目录中,我运行'react-native link',我在控制台中看到了这个:

rnpm-install info Platform 'ios' module react-native-view-shot is already linked
rnpm-install info Platform 'android' module react-native-view-shot is already linked
rnpm-install info Linking assets to ios project
rnpm-install info Linking assets to android project
rnpm-install info Assets have been successfully linked to your project

这里给出了什么>?

Platform: MacOS,
Editor: VSCode,
react: "16.4.0",
react-native: "0.54.2",
react-native-view-shot: "^2.4.0",

【问题讨论】:

  • 它在开发版本上工作吗?或者尝试手动链接。
  • 在开发构建中工作正常仅在单元测试中给出警告。

标签: reactjs react-native jestjs


【解决方案1】:

你需要为这个库做一个模拟。

例如

  1. app中创建一个目录__mocks__
  2. 创建文件 react-native-view-shot.js
  3. 在文件中,添加以下内容:

    jest.mock('react-native-view-shot', () => {
      return {
        // whatever function you want to mock, e.g capture: jest.fn()
      };
    });
    

【讨论】:

  • 我正在这样做。我的 mocks 文件夹在 tests 文件夹内
  • 尝试将其添加到 app 文件夹中,看看是否有帮助
猜你喜欢
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 2018-11-02
  • 2019-05-06
  • 2014-10-12
  • 2019-06-19
  • 1970-01-01
  • 2021-08-04
相关资源
最近更新 更多