gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 787 forks source link

Fix percent calculation #715

Open ChuntaoLu opened 7 years ago

ChuntaoLu commented 7 years ago

current way of computing percentage leads to falsy 100% coverage when numbers are big, e.g.,

> function percent(covered, total) {
...         var tmp;
...         if (total > 0) {
.....             tmp = 1000 * 100 * covered / total + 5;
.....             return Math.floor(tmp / 10) / 100;
.....         } else {
.....             return 100.00;
.....         }
...     }
undefined
> percent(20000, 20001)
100

Proposed fix:

> function percent(covered, total) {
...         var tmp;
...         if (total > 0) {
.....             return Math.floor(10000 * covered / total) / 100;
.....         } else {
.....             return 100.00;
.....         }
...     }
undefined
> percent(20000, 20001)
99.99
> percent(200000, 200001)
99.99
coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.002%) to 97.521% when pulling d44c89e9eeee2dd1843d8421fc34c22baa7f9894 on ChuntaoLu:fix-percent into 89e338fcb1c8a7dea3b9e8f851aa55de2bc3abee on gotwarlost:master.