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

"else path not taken" but it was #865

Open ki9us opened 6 years ago

ki9us commented 6 years ago

I have a function with an options object as an argument. Each key in the object takes a boolean, defaulting to true. At first, I wrote it like this:

myFunction = function( options={
  // Defaults
  opt1: true,
  opt2: true,
  // ...
} ){
  if (options.opt1) {
    // Do stuff
  }
  if (options.opt2) {
    // Do other stuff
  }
  // ...
}

However, I noticed that if I called it like this:

myFunction({opt2:false})

...the options object was incomplete, leaving options.opt1 as undefined (and therefore falsy). So I changed my code to explicitly check if the option had been set to false. That way, the if statement would include these undefined options:

myFunction = function( options={
  opt1: true,
  // ...
} ){
  if (options.opt1!==false) { // Problem solved
    // Do stuff
  }
}

Now this might not be the best way to do things... and I am taking suggestions, but that's beside the point.

I ran some tests for possibilities where each option could be false:

// A test
myFunction({opt1:false})
// Another test
myFunction({opt2:false})

So opt2 defaults to true, erm, undefined in the first test and opt1 is true in the second. You see, I have followed both implicit else branches. The tests are running correctly, so I know the if-statements are working as intended.

Each if-statement is throwing the "else path not taken" error. Is it a bug?

Feel free to clone the entire repo to reproduce this.