Skip to content

Commit

Permalink
fix: Increase precision when calculating coverage percent (#594)
Browse files Browse the repository at this point in the history
Fixes #593
  • Loading branch information
jvanoostveen committed Feb 21, 2021
1 parent 20bfade commit 6ddb374
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/istanbul-lib-coverage/lib/percent.js
Expand Up @@ -7,7 +7,7 @@
module.exports = function percent(covered, total) {
let tmp;
if (total > 0) {
tmp = (1000 * 100 * covered) / total + 5;
tmp = (1000 * 100 * covered) / total;
return Math.floor(tmp / 10) / 100;
} else {
return 100.0;
Expand Down
18 changes: 18 additions & 0 deletions packages/istanbul-lib-coverage/test/percent.test.js
@@ -0,0 +1,18 @@
'use strict';
/* globals describe, it */

const assert = require('chai').assert;
const percent = require('../lib/percent');

describe('percent calculation', () => {
it('calculates percentage covered and total', () => {
const p = percent(1, 1);

assert.equal(p, 100);
});
it('calculates percentage with enough precision to detect minor differences with large covered and total', () => {
const p = percent(999998, 999999);

assert.isBelow(p, 100);
});
});
@@ -1,6 +1,6 @@
{
"title": "100% line coverage, missing branch coverage",
"textReportExpected": "----------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|-------------------\n\u001b[32;1mAll files\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 95.35\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[33;1m \u001b[0m \n\u001b[32;1m index.js\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 95.35\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[33;1m21,29 \u001b[0m \n----------|---------|----------|---------|---------|-------------------\n",
"textReportExpected": "----------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|-------------------\n\u001b[32;1mAll files\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 95.34\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[33;1m \u001b[0m \n\u001b[32;1m index.js\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 95.34\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[33;1m21,29 \u001b[0m \n----------|---------|----------|---------|---------|-------------------\n",
"htmlSpaFiles": ["index.js.html", "index.html"],
"htmlSpaCoverageData": {
"file": "",
Expand All @@ -19,7 +19,7 @@
"covered": 41,
"missed": 2,
"skipped": 0,
"pct": 95.35,
"pct": 95.34,
"classForPercent": "high"
},
"functions": {
Expand Down Expand Up @@ -57,7 +57,7 @@
"covered": 41,
"missed": 2,
"skipped": 0,
"pct": 95.35,
"pct": 95.34,
"classForPercent": "high"
},
"functions": {
Expand Down
@@ -1,6 +1,6 @@
{
"title": "missing line and branch coverage",
"textReportExpected": "----------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|-------------------\n\u001b[32;1mAll files\u001b[0m | \u001b[32;1m 98.04\u001b[0m | \u001b[32;1m 95.35\u001b[0m | \u001b[32;1m 88.89\u001b[0m | \u001b[32;1m 97.87\u001b[0m | \u001b[31;1m \u001b[0m \n\u001b[32;1m index.js\u001b[0m | \u001b[32;1m 98.04\u001b[0m | \u001b[32;1m 95.35\u001b[0m | \u001b[32;1m 88.89\u001b[0m | \u001b[32;1m 97.87\u001b[0m | \u001b[31;1m9 \u001b[0m \n----------|---------|----------|---------|---------|-------------------\n",
"textReportExpected": "----------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|-------------------\n\u001b[32;1mAll files\u001b[0m | \u001b[32;1m 98.03\u001b[0m | \u001b[32;1m 95.34\u001b[0m | \u001b[32;1m 88.88\u001b[0m | \u001b[32;1m 97.87\u001b[0m | \u001b[31;1m \u001b[0m \n\u001b[32;1m index.js\u001b[0m | \u001b[32;1m 98.03\u001b[0m | \u001b[32;1m 95.34\u001b[0m | \u001b[32;1m 88.88\u001b[0m | \u001b[32;1m 97.87\u001b[0m | \u001b[31;1m9 \u001b[0m \n----------|---------|----------|---------|---------|-------------------\n",
"htmlSpaFiles": ["index.js.html", "index.html"],
"htmlSpaCoverageData": {
"file": "",
Expand All @@ -11,23 +11,23 @@
"covered": 50,
"missed": 1,
"skipped": 0,
"pct": 98.04,
"pct": 98.03,
"classForPercent": "high"
},
"branches": {
"total": 43,
"covered": 41,
"missed": 2,
"skipped": 0,
"pct": 95.35,
"pct": 95.34,
"classForPercent": "high"
},
"functions": {
"total": 9,
"covered": 8,
"missed": 1,
"skipped": 0,
"pct": 88.89,
"pct": 88.88,
"classForPercent": "high"
},
"lines": {
Expand All @@ -49,23 +49,23 @@
"covered": 50,
"missed": 1,
"skipped": 0,
"pct": 98.04,
"pct": 98.03,
"classForPercent": "high"
},
"branches": {
"total": 43,
"covered": 41,
"missed": 2,
"skipped": 0,
"pct": 95.35,
"pct": 95.34,
"classForPercent": "high"
},
"functions": {
"total": 9,
"covered": 8,
"missed": 1,
"skipped": 0,
"pct": 88.89,
"pct": 88.88,
"classForPercent": "high"
},
"lines": {
Expand Down

0 comments on commit 6ddb374

Please sign in to comment.