简体   繁体   中英

optional chaining operator jest testing

Newer to jest testing and reaching out to the community 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}
/>

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"
   ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM