Last active
September 25, 2018 19:46
-
-
Save nikkomiu/def79321643fed6d623ae0f663734f50 to your computer and use it in GitHub Desktop.
Jest Coverage Helper for Gitlab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
... | |
"reporters": ["default", "<rootDir>/test-helpers/simple-text-summary.js"], | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fs = require('fs'); | |
const path = require('path'); | |
const { parseString } = require('xml2js'); | |
const coverageFile = path.join(__dirname, '../coverage/clover.xml') | |
const coverageReader = (err, { coverage }) => { | |
if (err) { | |
console.error('Could not process XML!\n'); | |
console.error(err.toString()); | |
return; | |
} | |
const { | |
statements, | |
coveredstatements, | |
conditionals, | |
coveredconditionals, | |
methods, | |
coveredmethods, | |
elements, | |
coveredelements | |
} = coverage.project[0].metrics[0]['$']; | |
const statementPercent = +coveredstatements / +statements; | |
const conditionalPercent = +coveredconditionals / +conditionals; | |
const methodPercent = +coveredmethods / +methods; | |
const elementPercent = +coveredelements / +elements; | |
const sum = statementPercent + conditionalPercent + methodPercent + elementPercent; | |
// Total is (sum / 4) * 100 which translates to * 25 | |
console.log(`Total Coverage: ${(sum * 25).toFixed(2)}%`) | |
}; | |
fs.readFile(coverageFile, 'utf8', async (err, data) => { | |
if (err) { | |
console.error('Could not process coverage results.'); | |
console.error('Make sure the "clover" reporter is enabled and the file has been written.\n'); | |
console.error(err.toString()); | |
return; | |
} | |
parseString(data, coverageReader); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment