Sonar doesn't respect istanbul ignore comments

Version: Sonar Scanner 4.6 and hosted sonarcloud.io

Reproduction steps

// a.ts
export const foo = () => {
  return 1;
}

/* istanbul ignore next */
export const bar = () => {
  return 2;
}
// b.ts
/* istanbul ignore file */
export const baz = () => {
  return 3;
}
// a.test.ts
import { foo } from './a.ts.';

it('foo', () => {
  expect(foo()).toEqual(1);
});
  • create a simple TypeScript project that uses jest, with two source files and 1 test, as illustrated above:
    • the test partially covers a.ts, and none of b.ts
    • b.ts contains /* istanbul ignore file */ comment at the top of the file
    • alternatively, it can contain /* istanbul ignore next */ before the function block
  • generate an LCOV coverage report using jest and the lcov reporter
  • upload the report with Sonar Scanner using sonar.javascript.lcov.reportPaths

Expected

  • a.ts should report 100% coverage because the foo function is covered by tests, while the bar function is ignored through an istanbul comment
  • b.ts should not be included in the coverage report because it is ignored through an istanbul comment, or it should report 100%

Actual

Coverage for a.ts is reported as expected, but b.ts shows 0%, not respecting the istanbul comments. Both file and next comments do not have any effect.

Notes

istanbul is the tool used to generate coverage reports for JS/TS. It supports ignoring parts of the code using special comments: istanbul/ignoring-code-for-coverage.md at master · gotwarlost/istanbul · GitHub

Hi,

This is about Executable Lines. This blog post from when we first introduced the feature should help.

 
Ann

Hi @ganncamp, in my case I am explicitly marking a file to be ignored, and jest (which is setup to cover files that are not tested, same as described in your blog post) correctly ignores it. Sonar not respecting the coverage tool hints is a bug IMO.

The only option today is to duplicate the “istanbul” information to the property sonar.coverage.exclusions by listing all the files you expect to be ignored from a coverage perspective.

@Alexandre_Gigleux that would add an extra maintenance cost. Do you plan to support the istanbul comments any time soon?

Better question, do you plan to add an option in Sonar to turn off Executable Lines? jest can be configured to report coverage from untested files and it will respect the istanbul ignore comments.

Hi Andrei,

We don’t have any plans currently to support this. Having said that, you can make a feature suggestion here and product management will be alerted to the idea: