【发布时间】: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
环境:
- 反应原生:0.67.0
- 反应原生元素:3.4.2
- @testing-library/jest-native: 4.0.4
- @testing-library/react-native: 9.0.0
- 开玩笑:27.4.7
- metro-react-native-babel-preset: 0.66.2
- 反应测试渲染器:17.0.2
- @babel/core: 7.16.7
- @babel/runtime: 7.16.7
- 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