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

标签 react-native jestjs react-navigation

我一直在尝试在我的新 React Native 应用程序中设置 react-navigation 。虽然我的代码/屏幕正在工作,但 Jest 在按照 v4 docs 添加库后给我带来了问题。 。

我安装了react-navigationreact-native-reanimatedreact-native-gesture-handlerreact- native 屏幕@^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(<App />)

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
      at Object.<anonymous> (__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 配置:

{
  "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/.*))"
    ]
  }
}

最佳答案

解决此问题的当前方法

更新于 2021 年 5 月 18 日

在最新版本的 React Native 中,NativeAnimatedHelper 的位置似乎已移动。

改变

jest.mock('NativeAnimatedHelper');

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

<小时/>

以前解决此问题的方法

改变

jest.mock('NativeAnimatedHelper');

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

关于react-native - 使用 Jest 模拟时找不到模块 'NativeAnimatedHelper',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257932/

相关文章:

react-native - 如何在React Native中从heic格式获取jpg图像

javascript - 开 Jest : Difference betwen --runInBand and --maxWorkers 1

reactjs - 为什么调试器要求我再次弹出?

javascript - 当所有屏幕通过 React Navigation 获得焦点时,在每个屏幕上执行相同的功能

javascript - 登录成功后如何跳转?在 native + 世博会

javascript - 阻止 React Native WebView 访问 YouTube 网站

react-native - console.error : "fontFamily "Material Icons"is not a system font and has not been loaded through Font. loadAsync

react-native - React Native 错误 - ListView 已从 ReactNative 中删除

javascript - 属性的单元测试失败,只能在单个节点上运行?

react-native - 我如何在 native react 中获取路线参数