Jestjs Jest testcase在分析react native中的流类型时由于意外标记而失败

Jestjs Jest testcase在分析react native中的流类型时由于意外标记而失败,jestjs,flowtype,Jestjs,Flowtype,我正在尝试使用我的react本机应用程序设置jest。我创建了一个简单的测试用例,在运行npm测试时,出现了以下错误 FAIL __tests__/actionsSpecs.js ● Test suite failed to run /Users/abc/Projects/MyApp/node_modules/react-native/Libraries/Renderer/src/renderers/shared/hooks/ReactHostOperationHistoryH

我正在尝试使用我的react本机应用程序设置jest。我创建了一个简单的测试用例,在运行
npm测试时,出现了以下错误

 FAIL  __tests__/actionsSpecs.js
  ● Test suite failed to run

    /Users/abc/Projects/MyApp/node_modules/react-native/Libraries/Renderer/src/renderers/shared/hooks/ReactHostOperationHistoryHook.js: Unexpected token (20:4)
        18 |   & {instanceID: DebugID}
        19 |   & (
      > 20 |     | {type: 'mount', payload: string}
           |     ^
        21 |     | {type: 'insert child', payload: {toIndex: number, content: string}}
        22 |     | {type: 'move child', payload: {fromIndex: number, toIndex: number}}
        23 |     | {type: 'replace children', payload: string}
基本上,它在流类型声明上失败了。我尝试过使用
flow
babel预设和
transformflow strip类型
,但没有任何帮助

下面是我在package.json中的jest配置

"jest": {
    "preset": "react-native",
    "setupFiles": [
      "./setup.js"
    ],
    "transformIgnorePatterns": [ "node_modules/(?!react-native)" ]
  }
这是
.babelrc
文件

{
  "presets": ["react-native", "flow"]
}
看起来
预设在传输过程中没有去除流类型,但我现在不知道如何让它工作


如果您知道有什么问题,请告诉我。

您需要的不仅仅是预设,您还需要一个转换来剥离流类型。通常这是通过babel插件完成的

您的
.babelrc
文件应如下所示:

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    "transform-flow-strip-types"
  ]
}

编辑:另一种可能是流语法不正确,转换不知道如何处理它。在我看来,从您粘贴的代码片段来看,在第20行的对象定义之前有一个额外的
。删除该选项是否会改变输出?

我已尝试使用
预设和
变换流条类型
,但在这两种情况下,我得到的错误与上述相同。我想问题在于
babel
没有起作用,因为它们中的任何一个都应该解决它。你能分享你的包.json的
jest
部分吗?我已经有这个问题了。
transformIgnorePatterns
是我从围绕类似主题的各种问题中得到的。我尝试过使用和不使用
transformIgnorePatterns
的方法,但都没有效果。哦,对不起,你说得对!我错过了。我这样问是因为如果您在jest配置中有
“transform”:{}
,它将忽略所有转换。我已经用另一种可能性更新了上面的答案。报告此错误的代码是
react native
code,并且根据文档,流声明是正确的。我以前使用的是node v5.9.1,我更新到node v8.2.1并重新安装了所有node_模块,这似乎已经修复了错误。不确定节点版本如何影响流声明。