热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

使用Jest模拟时找不到模块'NativeAnimatedHelper'

如何解决《使用Jest模拟时找不到模块'NativeAnimatedHelper'》经验,为你挑选了1个好方法。

我一直在尝试react-navigation在新的React Native应用中进行设置。当我使代码/屏幕正常工作时,按照v4 docs在库中添加后,Jest导致了我的问题。

我安装react-navigationreact-native-reanimatedreact-native-gesture-handler,和react-native-screens@^1.0.0-alpha.23。然后,我进入ios文件夹并运行pod install。因为我使用的是React Native v0.61,所以它们应该已经自动链接了。

当我运行默认的Jest测试时,出现如下错误:

  ? renders correctly (106ms)

  console.warn node_modules/react-native/Libraries/Animated/src/NativeAnimatedHelper.js:270
    Animated: `useNativeDriver` is not supported because the native animated module is missing. Falling back to JS-based animation. To resolve this, add `RCTAnimation` module to this app, or remove `useNativeDriver`. More info: https://github.com/facebook/react-native/issues/11094#issuecomment-263240420

我搜索了此错误,大多数建议的答案是将其放入测试文件中:

jest.mock('NativeAnimatedHelper');

但是,当我尝试这样做时,Jest抱怨如下错误:

 FAIL  __tests__/App-test.tsx
  ? Test suite failed to run

    Cannot find module 'NativeAnimatedHelper' from 'App-test.tsx'

       6 | import renderer from 'react-test-renderer'
       7 |
    >  8 | jest.mock('NativeAnimatedHelper')
         | ^
       9 |
      10 | it('renders correctly', () => {
      11 |   renderer.create()

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
      at Object. (__tests__/App-test.tsx:8:1)

我一直在尝试各种方法,任何帮助/建议将不胜感激!


版本:

Node: 10.16.1
npm: 6.11.3
"react": "^16.9.0",
"react-native": "0.61.1",
"react-native-gesture-handler": "^1.4.1",
"react-native-reanimated": "^1.3.0",
"react-native-screens": "^1.0.0-alpha.23",
"react-navigation": "^4.0.10",
"react-navigation-stack": "^1.9.3",

"babel-jest": "^24.1.0"
"jest": "^24.1.0"
"react-test-renderer": "^16.9.0"

开玩笑的配置:

{
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "setupFiles": [
      "./node_modules/react-native-gesture-handler/jestSetup.js"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|react-navigation|@react-navigation/.*))"
    ]
  }
}

小智.. 6

更改

jest.mock('NativeAnimatedHelper');

jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');



1> 小智..:

更改

jest.mock('NativeAnimatedHelper');

jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');


推荐阅读
author-avatar
白色的睡
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有