Codecov always shows 100% incorrectly
Codecov incorrectly shows 100% coverage#
You know what's worse than no test coverage? A lie that it's 100% covered!
When you run nyc mocha test
, you get this:
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 75 | 75 | 100 | 75 | |
index.js | 75 | 75 | 100 | 75 | 6,14 |
----------|----------|----------|----------|----------|-------------------|
A report of 75% test coverage.
However, uploading the report to Codecov reports 100% test coverage - a blatantly wrong report!
Why is this happening?
Do you have code which has a condition and statement on the same line? Something like this?
if (!input) throw new Error('Missing arguments');
return input + input;
Some test coverage reporters cannot see statements on the same line as a condition and will generate a wrong report, therefore use curly brackets with conditions. Besides, this approach results in neater difference comparision in version control tools like Git.
Change the code above to:
if (!input) {
throw new Error('Missing arguments');
}
return input + input;
The generated report will now be accurate and Codecov will show the correct 75% test coverage.
Summary#
Don't put statements on the same line as a condition. Put the statement within the curly brackets of the condition.