Update karma coverage threshold automatically
In previous post we saw how we can define the karma coverage threshold, using the magic check
command:
coverageReporter: {
check: {
global: {
statements: 82,
lines: 67,
functions: 100,
branches: 85
}
}
}
This way the test run will fail if the code coverage get’s below your threshold.
Note! This way you can also prevernt silly unfortunate mistakes, such as committing the f word (
fdescribe
orfit
)…
Show Must Go On…
Well, life happens, and you continue with your project.
You’ve been a very good boy, and your coverage is now better.
You want to update you’re coverage threshold to the new standard.
Manual Labor?
No.
Threshold Outsourcing
First, save your threshold in a separate file:
coverageReporter: {
check: {
global: require('./coverage.conf.json')
}
}
Now, your coverage.conf.json will look like this:
{
"statements": 68,
"branches": 49,
"functions": 61,
"lines": 68
}
Json Coverage Report
It turns out that you also have a json reporter and a json-summary reporter. Unfortunately, it still doesn’t show up in karma-coverage docs yet (I’ve opened a PR).
Try this:
reporters: [
{type: 'json-summary'}
]
You’ll get a beautiful json, with your summary coverage.
Define New Goals
First, let’s get the last run coverage summary:
//read the "coverage-summary.json" file
let fileContent = fs.readFileSync('coverage-summary.json');
//parse string to json
let coverageSummary = JSON.parse(fileContent);
Then, extract the percentage results.
We’de like to add a FLEX
factor, to reduce the roughness of the latest coverage results (I’ve set it to 0.5
):
//define new thresholdConfig
let thresholdConfig = {};
//threshold factors
let factors = ['lines', 'statements', 'branches', 'functions'];
//extract data from coverageSummary to thresholdConfig
//FLEX is a number to have some flexibility
factors.forEach(key =>
thresholdConfig[key] = coverageSummary.total[key].pct - FLEX
);
Now, you can define you new threshold.
//overwrite the thresholdConfig to "coverage.conf.json"
fs.writeFileSync('coverage.conf.json',
JSON.stringify(thresholdConfig),
'utf8');
CI/CD
The next stage would be to add a stage to you CI/CD cycle:
- commit
- run test
- update thresholds
- commit “coverage.conf.json” (but don’t rerun tests)