Code Coverage calculation in Sonar

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


package.json scripts

"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)

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%

1 Like

Thanks for the explanation.