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.

Coverage doesn't have "conditional assignment" into consideration

See original GitHub issue

🐛 Bug Report

When an object is set like this:

const myObj = {
  myProp: myVariable === 'given expected value',
};

Jest (or maybe nyc?) is not considering it as a branch.

However if I do:

const myObj = {
  myProp: myVariable === 'given expected value' ? true : false,
};

This is considered as a branch…

To Reproduce

Steps to reproduce the behavior:

  • yarn create react-app
  • yarn add classnames
  • Change the src/App.js to:
import classnames from 'classnames';
import React from 'react';
import logo from './logo.svg';
import './App.css';

function App({ test }) {
  const myClassName = {
    [test]: test === 'yeah'
  };

  return (
    <div className={classnames('App', myClassName)}>
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;
  • Change the src/App.test.js to:
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
  const { container, getByText } = render(<App />);
  const linkElement = getByText(/learn react/i);
  expect(linkElement).toBeInTheDocument();
  expect(container).toMatchSnapshot();
});
  • Run yarn test --coverage
  • Verify that the coverage is still 100%: image

Expected behavior

image

Link to repl or repo (highly encouraged)

https://github.com/mAiNiNfEcTiOn/jest-conditional-assignment-repro

envinfo


  System:
    OS: macOS 10.15.2
    CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
    Memory: 2.19 GB / 32.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.13.1 - /usr/local/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.12.1 - /usr/local/bin/npm
  Managers:
    Homebrew: 2.2.2 - /usr/local/bin/brew
    pip2: 19.3.1 - /usr/local/bin/pip2
    pip3: 19.3.1 - /usr/local/bin/pip3
    RubyGems: 3.0.3 - /usr/bin/gem
  Utilities:
    Make: 3.81 - /usr/bin/make
    GCC: 4.2.1 - /usr/bin/gcc
    Git: 2.21.0 - /usr/bin/git
    Clang: 1100.0.33.16 - /usr/bin/clang
    Subversion: 1.10.4 - /usr/bin/svn
  Servers:
    Apache: 2.4.41 - /usr/sbin/apachectl
  Virtualization:
    Docker: 19.03.5 - /usr/local/bin/docker
    VirtualBox: 6.0.14 - /usr/local/bin/vboxmanage
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
  IDEs:
    Nano: 2.0.6 - /usr/bin/nano
    VSCode: 1.41.1 - /usr/local/bin/code
    Vim: 8.1 - /usr/bin/vim
    Xcode: 11.3/11C29 - /usr/bin/xcodebuild
  Languages:
    Bash: 3.2.57 - /bin/bash
    Perl: 5.18.4 - /usr/bin/perl
    PHP: 7.3.9 - /usr/bin/php
    Python: 2.7.16 - /usr/bin/python
    Python3: 3.7.5 - /usr/local/bin/python3
    Ruby: 2.6.3 - /usr/bin/ruby
  Databases:
    SQLite: 3.28.0 - /usr/bin/sqlite3
  Browsers:
    Chrome: 79.0.3945.88
    Chrome Canary: 81.0.4020.0
    Firefox: 71.0
    Safari: 13.0.4

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
jeysalcommented, Feb 1, 2020

I also think you may be overusing the coverage metric here, it can never tell you if you’re actually testing all relevant cases on its own. Code reviews are still required so that a human can spot at the usage of classnames that this piece of code requires a test case for “has the class” and one for “does not have the class” 😃

0reactions
github-actions[bot]commented, May 11, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code coverage testing, when enough is enough - JS.dev
Given a boolean for example, to ensure coverage for both states of the property - true and false - at least two different...
Read more >
Devel::Cover Branch coverage on conditional ternary operator
So there is a branch there that has not been covered by the test. You have several options: go with the empty list...
Read more >
Conditional Operator - an overview | ScienceDirect Topics
VHDL. Conditional signal assignments perform different operations depending on some condition. They are especially useful for describing a multiplexer. For ...
Read more >
Assignment of Benefits - Florida Office of Insurance Regulation
An Assignment of Benefits, or an AOB, is a document signed by a policyholder that allows a third party, such as a water...
Read more >
How to Easily Understand Your Insurance Contract
For example, in auto insurance, if you lend your car to a friend who doesn't have a license and that friend is involved...
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