gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 787 forks source link

Incorrect branch coverage logic with parameter and destructuring defaults #779

Open getify opened 7 years ago

getify commented 7 years ago

Take this code as a minimal test case:

function foo(x = 2) {
   console.log(x);
}

foo();

If you run Istanbul (1.0 alpha1) against it, you get a 100% passing report for all parts.

However, there's a branch that wasn't tested here, the implied non-default branch. The only branch tested is the invoking of the = 2, but there's no testing of what happens when an argument is passed affirmatively for the x parameter. Istanbul should complain that this implied branch isn't being tested.

To illustrate, let me show you the equivalent code pre-ES6:

function foo(x) {
   x = x !== undefined ? x : 2;
   console.log(x);
}

foo();

As you can clearly see, there's definitely two branches, and only one of them is getting tested. In fact, Istanbul will correctly report 50% branch coverage here.

Of course, the same reasoning goes for destructuring defaults:

var { x = 2 } = { };

Istanbul calls that 100% covered, when it should report the implied non-default branch not being covered.

var tmp = { };
var x = tmp.x !== undefined ? tmp.x : 2;

This equivalent is correctly reported as only 50% covered.