node-cover icon indicating copy to clipboard operation
node-cover copied to clipboard

Add decision/condition coverage

Open goatslacker opened this issue 13 years ago • 9 comments

goatslacker avatar Mar 01 '12 18:03 goatslacker

Can you give an example of what you want here? What statistics do you want to collect?

itay avatar Mar 01 '12 18:03 itay

Here's a simple example:

var a;
if (true) {
  a = 1;
}

This will report as 100% coverage. I covered all lines and all blocks however I did not cover all branches. What if the if statement evaluates to false? That is currently not being reported.

Here's another example with ternary operator:

function what(yes) {
  return yes ? 1 : 2;
}

// test:
what(true);

cover tells me it's 100% coverage when in fact I'm not covering return 2

goatslacker avatar Mar 01 '12 21:03 goatslacker

In the first case, you are covering all branches, no? Your 'if' condition evaluates to true. I agree that if it was false, you would have had less than 100% coverage, but I think it would report it as less than a 100%, no?

For the second case, you're right, we're misreporting to some degree. If you had return yes ? a() : b(), we'd report it more correctly.

I think I have a fix in mind to give more correct stats for some of these cases. Open to ideas/suggestions.

itay avatar Mar 01 '12 22:03 itay

Can you try installing the new version (0.2.1)? I think I resolved the issue.

itay avatar Mar 01 '12 22:03 itay

The first case was a trivial example. But yes, I do in fact have 100% line coverage but my testing code is "flawed" in the sense that I'm not ever checking if (false)

Btw I don't mind checking out and building from git so you don't have to be publishing packages to npm.

goatslacker avatar Mar 01 '12 23:03 goatslacker

Thanks - let me know if the fix worked for you :)

itay avatar Mar 01 '12 23:03 itay

So, the fix worked fine for the ternary operator example. If I were to nitpick though, it told me I missed a line when it should really tell me that I missed a branch

goatslacker avatar Mar 02 '12 01:03 goatslacker

I see. There are a couple of ways to look at it:

In this case, it's telling you that you missed a partial line, because you covered some parts of the line but not others (that's why it is in yellow). There are other cases that aren't really "branches". Consider for example: if (true || 2) { .. }. In this case, you miss the 2, because the true will always evaluate.

I do think that it might be interesting to add secondary stats beyond line and block coverage. For example, we could add function-coverage and branch-coverage.

I'll think about adding those. Any suggestions are welcome!

itay avatar Mar 02 '12 05:03 itay

function coverage would be great. I'll keep posting more issues as I find them.

goatslacker avatar Mar 02 '12 05:03 goatslacker