react-native - Jest TransformIgnorePatterns 所有 node_modules 用于 React-Native Preset
问题描述
我是开玩笑的新手,
在我的项目的 package.json 中设置 jest 配置后,
包.json
"jest": {
"preset": "react-native",
"verbose": true,
"moduleDirectories": ["node_modules", "src"],
"transformIgnorePatterns": ["node_modules/(?!(react-native-cookies)/)"]
},
我已经尝试过忽略所有节点模块:-
"transformIgnorePatterns": ["node_modules"]
但对我不起作用
和.babelrc
{
"presets": ["react-native"]
}
我的 LoginScreen-Test.js 代码:-
测试用例
import 'react-native';
import React from 'react';
import LoginScreen from '../src/components/LoginScreen';
import renderer from 'react-test-renderer';
it('renders correctly', () => {
const hello = renderer.create(<LoginScreen/>).toJSON();
expect(hello).toMatchSnapshot();
});
我开始运行 --> npm test 或 npm test -- -u
它向我反映了以下错误:-
终端输出
FAIL 测试/LoginScreen-test.js ● 测试套件未能运行
/Users/Documents/Projects/node_modules/react-native/Libraries/Utilities/Platform.ios.js:31 get isTesting(): boolean { ^ SyntaxError: Unexpected token : at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:316:17) at Object.get Platform [as Platform] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:111:27) at Object.<anonymous> (node_modules/react-native-cookies/index.js:9:17)
我想通过 TransformIgnorePattern 忽略所有节点模块,但似乎它不适用于我的 React-Native Preset,..
寻找有用的答案...
解决方案
此错误表明“react-native”尚未转换:
react-native/Libraries/Utilities/Platform.ios.js:31
"transformIgnorePatterns": ["node_modules"]
不起作用,因为它几乎是默认行为。
您是否尝试过使用官方推荐配置?对于您的项目,它应该看起来像这样:
"transformIgnorePatterns": [
"node_modules/(?!(react-native|react-native-cookies)/)"
]
这?!
很重要,因为它意味着忽略 node_modules 中的所有内容,除了react-native
and react-native-cookies
。
推荐阅读
- c++ - 从数组中读取整数时得到意外的数字
- java - 尝试编写使用开关从 sqlite 数据库中选择数据的 java 代码
- javascript - 输入字段值采取的状态不能被修改
- python - 在 virtualenv/Pipenv 中运行时如何指向相对于 Python 文件的文件夹?
- javascript - 无法在 react-native 0.63.4 (Android) 中发出任何网络请求(获取)
- r - if else 循环创建一个新的分类变量
- python - 如何使用 Selenium 和 Python 按类识别元素?
- ruby-on-rails - Rails 引擎中的关联不起作用
- html - 如何在使用 css 转换(平移或/和缩放)和浮点值时修复“边框周围的边框”错误?
- node.js - JSON Web Token (JWT) 太长无法通过客户端 URL?Node.js 和 Express 身份验证