【问题标题】:Can't convert node without a body无法转换没有主体的节点
【发布时间】:2019-02-10 20:36:02
【问题描述】:

我正在尝试在react-testing-library 中运行the example 来测试反应挂钩。但它似乎在这条线上失败了:

testHook(() => ({count, increment} = useCounter({ initialCount: 2 })))

看起来它与 Babel 有关。尝试谷歌搜索,但找不到任何似乎有同样问题的人。我用create-react-app 安装了webpack。

这是堆栈跟踪:

 ● Test suite failed to run

  Can't convert node without a body

  at NodePath.ensureBlock (node_modules/@babel/traverse/lib/path/conversion.js:64:11)
  at Scope.push (node_modules/@babel/traverse/lib/scope/index.js:727:12)
  at Object.toSequenceExpression (node_modules/@babel/types/lib/converters/toSequenceExpression.js:19:11)
  at NodePath.replaceExpressionWithStatements (node_modules/@babel/traverse/lib/path/replacement.js:203:36)
  at NodePath.insertAfter (node_modules/@babel/traverse/lib/path/modification.js:128:17)
  at NodePath.replaceWithMultiple (node_modules/@babel/traverse/lib/path/replacement.js:85:22)
  at PluginPass.AssignmentExpression (node_modules/babel-preset-react-app/node_modules/@babel/plugin-transform-destructuring/lib/index.js:433:14)
  at newFn (node_modules/@babel/traverse/lib/visitors.js:193:21)
  at NodePath._call (node_modules/@babel/traverse/lib/path/context.js:53:20)
  at NodePath.call (node_modules/@babel/traverse/lib/path/context.js:40:17)

【问题讨论】:

  • 我不确定如何解决 Babel 问题,但如果你写 testHook(() => { return ({count, increment} = useCounter({ initialCount: 2 }))}),它可以工作。可能他们正在使用一些您当前设置没有的高级 ES6 语法糖。
  • @tuchi35 是的,这对我也有用。如果您将此作为答案发布,我很乐意接受并投票。

标签: javascript reactjs babeljs create-react-app react-testing-library


【解决方案1】:

我不确定如何解决 Babel 问题,但如果你写的话:

testHook(() => { return ({count, increment} = useCounter({ initialCount: 2 }))})

代替:

testHook(() => ({count, increment} = useCounter({ initialCount: 2 })))

它有效。可能他们正在使用一些您当前设置没有的高级 ES6 语法糖。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多