Search code examples
javascriptreactjsbabeljscreate-react-appreact-testing-library

Can't convert node without a body


I'm trying to run the example in react-testing-library to test react hooks. But it seems to fail on this line:

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

Looks like it's related to Babel. Tried googling but cannot find anybody that seem to have the same problem. I installed webpack with create-react-app.

This is the stacktrace:

 ● 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)

Solution

  • I'm not sure how to fix the Babel issue but if you write:

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

    instead of:

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

    It works. Probably they're using some advance ES6 syntax sugar that your current setup doesn't have. Hope it helps!