Code Coverage calculation in Sonar

Hi everyone, I have a NodeJS 14 project on Sonar Enterprise 9.4 (scanner version 4.7.0.2747),
Here is the config for scanner and also for the test using mocha and istanbul.

sonar.sources="lib"
sonar.language="js"
sonar.javascript.lcov.reportPaths="coverage/lcov.info"
sonar.exclusions="lib/gen-nodejs/**,lib/payload/protobuf/Gateway_pb.js,examples/**,node_modules/**,test/**,"

package.json scripts

```json
"scripts": {
 "test": "istanbul cover --report html node_modules/.bin/_mocha --  -t 600000 recursive $(find ./test -path '*.js')",
"cover": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -u exports -R spec" ,
"cobertura": "./node_modules/.bin/istanbul report --dir ./coverage/report cobertura"
}

According the docs Sonar uses the following model to calculate coverage:

Coverage = (CT + CF + LC)/(2*B + EL)
where

CT = conditions that have been evaluated to ‘true’ at least once
CF = conditions that have been evaluated to ‘false’ at least once
LC = covered lines = linestocover - uncovered_lines
B = total number of conditions
EL = total number of executable lines (lines_to_cover)

Following the formula above, how would fit for the file below

file measures

CT+CF = 1 (2 Conditions to Cover - 1 Uncovered Conditions)
LC = 7 (8 Lines to Cover - 1 Uncovered Line)
2*B = 2 (Conditions to Cover already represents the product)
EL = 8 (8 Lines to Cover)

(1 + 7)/(2+8)

8/10 = 80%

Thanks for the explanation.

The formula makes sense, but I think the confusing part here is how SonarQube is interpreting the covered conditions and executable lines from the LCOV report. I’d first compare the lines, functions, and branches sections in coverage/lcov.info with the measures shown in SonarQube. That should help identify whether the difference comes from the report itself or from how SonarQube maps the coverage data to the source file. Checking a small example file manually can also make the calculation much easier to verify.