3

Problem

Is there a way to configure Istanbul/Jest to collect a separate code coverage metric?

The metric should count the visited lines/branches of code but only when the line is executed as a part of a direct call to containing function.

Motivation

I want to see how well are functions tested in isolation (in a true sense of unit-testing).

Example

For the code below, I am interested in seeing the metric that counts every line of foo() function as visited once (since it is invoked directly from the test); however each bar()'s line to not be counted, because it's not invoked directly by the test, but rather called transitively.

.
    // test.js       
    it(`should count things well`, () => {
      expect(foo()).toEqual(3);
    });
    
    // code.js
  | function foo() {
1 |   const a = 1;
1 |   const b = bar();
1 |   return a + b;
  | }

  | function bar() {
0 |   return 2;
  | }

What I tried

The Istanbul-generated coverage-final.json does not seem to contain any metadata about what a certain line of code is. It only tells whether certain lines/branches of code are visited or not. I wonder if there are some command flags I could use to control what coverage information is being emitted and make an analysis of the extended data.

Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
  • 1
    Not exactly what you're looking for (so I won't say it's an "answer") but you could possibly mock everything by default (i.e. automock) then explicitly use the real function (i.e. requireActual) when you're testing it. If that works like I think it does you'll see zero coverage for functions that weren't called directly. – TastyWheat Mar 29 '21 at 19:55

0 Answers0