最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - optional chaining operator jest testing - Stack Overflow

programmeradmin4浏览0评论

Newer to jest testing and reaching out to the munity for direction. I need to write a jest test around the following block of code. The environment leverages jest, enzyme and chai. I have searched high and low for examples of optional chaining operator tests. Any examples or links anyone can provide to point me in the right direction.

<TableCell
   key={i}
   size={columnResizing?.columnWidths[column.id] || column.size}
/>

Newer to jest testing and reaching out to the munity for direction. I need to write a jest test around the following block of code. The environment leverages jest, enzyme and chai. I have searched high and low for examples of optional chaining operator tests. Any examples or links anyone can provide to point me in the right direction.

<TableCell
   key={i}
   size={columnResizing?.columnWidths[column.id] || column.size}
/>
Share Improve this question edited Sep 15, 2020 at 21:22 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Sep 15, 2020 at 19:07 BrownBrown 411 silver badge3 bronze badges 1
  • 2 where this columnResizing is ing from? from props, some function call or separate constant? in general you just need to make this columneResizing be undefined but how to achieve that depends on where it es from – skyboyer Commented Sep 15, 2020 at 21:25
Add a ment  | 

2 Answers 2

Reset to default 5

To enable optional-chaining, install it, like this:

yarn add @babel/plugin-proposal-optional-chaining --dev

After installing it, you need to ensure it's registered within your babel plugins section, like so:

{
  "plugins": ["@babel/plugin-proposal-optional-chaining"]
}

In case someone has a problem with optional chaining in jest + testing-library/react stack.

Test failed because of "Support for the experimental syntax 'optionalChaining' isn't currently enabled" can be resolved by adding

yarn add @babel/plugin-syntax-optional-chaining @babel/plugin-syntax-optional-chaining --dev

Add plugins inside .babelrc to enable transformation:

  "plugins": [
    "@babel/plugin-syntax-optional-chaining",
    "@babel/plugin-proposal-optional-chaining"
   ]
发布评论

评论列表(0)

  1. 暂无评论