将JEST与react原生元素一起使用-mockModal不是一个函数



是否可以在使用react-native-elements组件的react-native项目中使用jest?

示例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:reacttestingLibrarynode_modulesreact-native-elementsdistindex.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. @测试库/jest本机:4.0.4
  4. @测试库/本地react:9.0.0
  5. 笑话:27.4.7
  6. metro react原生babel预设值:0.66.2
  7. react测试渲染器:17.0.2
  8. @巴氏合金/芯:7.16.7
  9. @babel/运行时:7.16.7
  10. babel笑话:27.4.6

尝试使用此模拟

import .....
jest.mock('@rneui/themed', ()=>({
Button: jest.fn()
}));
describe( ... )
● Test suite failed to run
TypeError: mockModal is not a function
at node_modules/react-native/jest/setup.js:116:12

更新到react本机版本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

一个临时解决方法是将react本机降级为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

最新更新