reactjs - Jest 在用 jest 测试 react-native 时遇到了一个意外的令牌
问题描述
我想在我的 react-native 项目中使用 jest。我是开玩笑和反应原生的新手。目前低于错误
Details:
/Users/sachigrannan/SKO/Alpha4-1/node_modules/react-native-iphone-x-helper/index.js:1
import { Dimensions, Platform, StatusBar } from 'react-native';
^
SyntaxError: Unexpected token {
at Runtime._execModule (node_modules/jest-runtime/build/index.js:1157:58)
at Object.<anonymous> (node_modules/react-navigation-stack/lib/commonjs/vendor/TransitionConfigs/CardStyleInterpolators.tsx:2:1)
我已经在 package.json 中设置了这个。请帮忙!
"jest": {
"verbose": true,
"preset": "react-native",
"cacheDirectory": "./cache",
"coveragePathIgnorePatterns": [
"./app/utils/vendor"
],
"coverageThreshold": {
"global": {
"statements": 80
}
},
"transformIgnorePatterns": [
"node_modules/(?!(react-native|my-project|react-native-button|nodejs-mobile-react-native|react-navigation)/)"
]
}
解决方案
鉴于您的 transformIgnorePatterns 在引入 lib 之前工作,诀窍只是将“react-native-iphone-x-helper”添加到“transformIgnorePatterns”属性。
"node_modules/(?!(react-native|my-project|react-native-button|nodejs-mobile-react-native|react-navigation|react-native-iphone-x-helper)/)"
也就是说,您最好删除“transformIgnorePatterns”并让"preset": "react-native"
配置完成其工作。
我在预设中遇到的唯一问题是当我在嵌套的 node_module 目录中有一个未编译的依赖项时,例如。
/path/to/myproject/node_modules/@react-navigation/bottom-tabs/node_modules/react-native-iphone-x-helper/index.js:1
import { Dimensions, Platform, StatusBar } from 'react-native';
^
在这种情况下,问题是另一个模块使用了不同的版本,react-native-iphone-x-helper
因此我必须更新该依赖项以便它们对齐。如果这不可行,你有纱线的分辨率字段
推荐阅读
- python - 从 pandas 中的 read_csv 获取对象大小
- windows - 如何对标准输出进行非缓冲写入?
- google-apis-explorer - dialogflow projects.agent.intents.get 不起作用
- c# - 如何将使用数据模板动态创建的单选按钮组绑定到列表?
- javascript - Chrome 代理 api - 未捕获的类型错误:无效调用
- ios - 将多个图像下载到 CoreData
- javascript - HTML5 Canvas 背景和颜色的实时预览
- java - 如何解决 javax.net.ssl.SSLHandshakeException:握手期间远程主机关闭连接
- python - 从随机选择的行返回文件行位置
- regex - 可执行文件上的 Gitignore