【问题标题】:Using JEST with react-native-elements - mockModal is not a function将 JEST 与 react-native-elements 一起使用 - mockModal 不是函数
【发布时间】:2022-02-02 19:15:37
【问题描述】:

是否可以在 react-native 项目中使用 jest,这些项目使用来自 react-native-elements 的组件?

示例 jest 文件 https://github.com/vanGalilea/react-native-testing/blob/master/tests/Login.test.tsx 可以正常工作。一旦我将<Text> 组件从react-native 版本更改为react-native-elements 版本,我就会收到以下错误消息。

  ● Test suite failed to run

    Jest encountered an unexpected token
    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
    By default "node_modules" folder is ignored by transformers.
    Here's what you can do:
....
....
    Details:
    C:\react\testingLibrary\node_modules\react-native-elements\dist\index.js:6
    import Button from './buttons/Button';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

我在jest.config.js 文件中添加了以下行。

transformIgnorePatterns: ['node_modules/(?!(jest-)?@react-native|react-native|react-native-elements/*)',],

这消除了上面的错误。但是,现在,我收到以下错误消息。

  ● Test suite failed to run

    TypeError: mockModal is not a function

      at node_modules/react-native/jest/setup.js:116:12
      at Object.<anonymous> (node_modules/react-native/jest/mockModal.js:16:15)
      at node_modules/react-native/jest/setup.js:115:28

环境:

  1. 反应原生:0.67.0
  2. 反应原生元素:3.4.2
  3. @testing-library/jest-native: 4.0.4
  4. @testing-library/react-native: 9.0.0
  5. 开玩笑:27.4.7
  6. metro-react-native-babel-preset: 0.66.2
  7. 反应测试渲染器:17.0.2
  8. @babel/core: 7.16.7
  9. @babel/runtime: 7.16.7
  10. babel-jest:27.4.6

【问题讨论】:

  • 我刚刚从 0.66.4 升级到 0.67.1,并且进行了使用 react-native-elements 的测试,并且在升级之前它们通过了。我现在看到和你一样的错误,所以它似乎专门针对 0.67.x 而不是一般的 react-native。希望有人能给出答案!
  • 看起来这也是从 react-native 本身导入的 Modal 的问题:github.com/facebook/react-native/issues/32939

标签: react-native jestjs react-native-elements


【解决方案1】:
● Test suite failed to run

    TypeError: mockModal is not a function

      at node_modules/react-native/jest/setup.js:116:12

更新到 react-native 版本 0.67.2 以修复 mockModal 问题。这是来自 react-native 的更新: https://github.com/facebook/react-native/blob/main/CHANGELOG.md#fixed

问题: https://github.com/facebook/react-native/issues/32939

【讨论】:

    【解决方案2】:

    临时解决方法是将 react-native 降级为 0.66.4。这似乎可以解决问题。

    2022-01-21 更新:

    react-native-elements 开发团队承认了这个问题。此外,他们已经在测试版4.0.0-beta.0 中修复了它。检查以下问题以获取有关此错误的最新更新。 https://github.com/react-native-elements/react-native-elements/issues/3304

    【讨论】:

      猜你喜欢
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 2022-07-15
      • 2017-08-19
      相关资源
      最近更新 更多