question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

object destructuring defaults reported as uncovered branch

See original GitHub issue

Do you want to request a feature or report a bug? bug

What is the current behavior? The coverage reporter reports that the default values I assign in object destructuring are “not covered branches”.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test. Simply create a code that uses object destructuring defaults and check out the coverage report:

const {
  foo = 'bar'
} = this.props;

The = 'bar' segment will be considered “uncovered” by the coverage reporter.

What is the expected behavior?

The coverage reported will ignore it, since there’s nothing to test there.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

create-react-app config reproduces the problem just fine.

Moved from https://github.com/facebook/jest/issues/3091

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
mehmetnyararcommented, Aug 5, 2020

I encounter this issue. My fn:

export const getConfig = (env?: EnvVars, phase?: string): EnvConfig => {
  const {
    KEY1 = 'default',
    KEY2 = 'default',
    KEY3,
    KEY4,
    KEY5 = 10
  } = env || process.env

  // ...

  return config
}

My tests include:

  • getConfig() without initial env (defaults to process.env)
  • getConfig({}) empty env
  • getConfig({ KEY1: 'value', KEY2: 'value', KEY3: 'value', KEY4: undefined, KEY5: 20 }) with initial env

yet the de-structuring is not covered.

Is there any solution to this?

Putting /* istanbul ignore next */ comment before any de-structuring is not really a solution.

2reactions
akhileshwarrcommented, Jan 5, 2021

I faced same issue while destructuring props, culprit was react defaultProps, and code will never reach destructuring defaults.

const {  withCancel = false } = props;  // destructuring defaults are  not reachable

 ContentFooter.defaultProps = { // react defaultProps will set the value before it reaches destructuring defaults
  withCancel: false
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Uncovered Lines - How do I test these lines..?
In my case, a function had a default arg foo = false , it was being tested with different values for the arg,...
Read more >
Prevent Error with Default {} when Destructuring
When you use destructuring, make sure to set a default value of empty {} to prevent it from throwing an error! function hi(person)...
Read more >
Improve your component test coverage with this one weird trick
When it comes to unit testing, business logic is the most valuable area of your code to test. This was apparent on a...
Read more >
Let's Explore ES6 Destructuring in JavaScript - Morioh
Just as with object destructuring, you can set default values for local ... from report -l, --line It will print Lines & Uncovered...
Read more >
Code coverage report for runtime-core/src/apiSetupHelpers.ts
`<script setup>` components are closed by default - i.e. variables inside ... a proxy for the rest element when destructuring props with *...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found