istanbuljs/istanbuljs

Coverage utilizing 'istanbul ignore else' before 'else if' no longer working

sgerace opened this issue · 0 comments

We've been using the following syntax for quite some time to ignore the last 'else' branch in multi-else if statements without any issues:

function f(a) {
    if (a === 0) {
        console.log("0");
    } /* istanbul ignore else */
    else if (a === 1) {
        console.log("1");
    } else {
        console.log("2");
    }
}

f(0);
f(1);

Screen Shot 2022-01-27 at 11 34 44 AM

Notice how in the above case, we're hitting the first and second branches, but not the final else. However, the code coverage reports as we'd expect.

Today we started running into issues with our coverage reports after updating our node modules, however, the version of nyc was still the same version (15.1.0). After some investigation, it seems as though we had been using an older version of @babel/parser in our package-lock.json. Through a little trial and error, we were able to narrow down the version of @babel/parser that introduced the behavior change to 7.14.8. If we explicitly override the version of @babel/parser to 7.14.7 in our package.json, then we get the behavior shown above. However, if we set the version of @babel/parser to 7.14.8, then we get the following result instead:

Screen Shot 2022-01-27 at 11 34 23 AM

Here is the package.json that demonstrates the override:

{
  "name": "coverage-test",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "coverage": "nyc node ./example.js"
  },
  "devDependencies": {
    "nyc": "15.1.0"
  },
  "overrides": {
    "@babel/parser": "7.14.7"
  },
  "nyc": {
    "reporter": ["html"],
    "check-coverage": true,
    "statements": 100,
    "branches": 100,
    "functions": 100,
    "lines": 100
  }
}

Change "@babel/parser" from "7.14.7" to "7.14.8" and you will see the difference in behavior.

I'm not entirely sure what change in behavior is causing the ultimate issue in istanbuljs, but I figured this would be the best place to start since (a) I'm not sure whether or not the change in behavior in @babel/parser is actually incorrect, and (b) the unexpected behavior is observed through istanbul (via nyc).