fz-react-cli
Version:
A CLI tool for build modern web application and libraries
139 lines (131 loc) • 3.75 kB
JavaScript
let fs = require('fs');
let unitTestReport = [];
const result = function(inp) {
let testPathPattern = process.argv[process.argv.length - 1];
if (testPathPattern.indexOf('--') != -1) {
testPathPattern = '';
} else {
testPathPattern = fs.realpathSync(process.cwd());
}
let testPathRegex = new RegExp(testPathPattern);
let testResults = inp.testResults;
let testFilesArr = [];
// for (var i = 0; i < testResults.length; i++) {
testResults.forEach((testResult, i) => {
let filePath = testResult.testFilePath;
if (!testPathRegex.test(filePath)) {
return;
}
filePath = filePath.replace('.spec', '');
filePath = filePath.replace('/__tests__', '');
filePath = filePath.replace('/__test__', '');
let fileJson = {};
fileJson.testPath = testResult.testFilePath;
fileJson.sourcePath = filePath;
fileJson.data = testResult;
testFilesArr.push(fileJson);
testResult.testResults &&
testResult.testResults
.filter(t => t.status == 'failed')
.forEach(t => {
unitTestReport.push({
title: t.title,
fullName: t.fullName,
filePath: testResult.testFilePath
});
});
});
let coverageSummary = fs
.readFileSync('./coverage/coverage-summary.json')
.toString();
if (coverageSummary.indexOf('\\') != -1) {
coverageSummary = coverageSummary.replace(/\\/g, '\\\\');
}
let coverageJson = JSON.parse(coverageSummary);
let linesPercent = 0;
let functionPercent = 0;
let statementPerment = 0;
let branchesPercent = 0;
for (var i = 0; i < testFilesArr.length; i++) {
let curSourceFile = testFilesArr[i].sourcePath;
let coverageData = coverageJson[curSourceFile];
if (coverageData == undefined) {
console.log(
'Can\'t able to find source for ' +
testFilesArr[i].testPath +
'\n Or The spec file getting failed during test. Please check the file name and the path is correct for test file or Make them pass.'
);
continue;
}
linesPercent += coverageData.lines.pct;
functionPercent += coverageData.functions.pct;
statementPerment += coverageData.statements.pct;
branchesPercent += coverageData.branches.pct;
}
let totalLinesPercent = (linesPercent / (i * 100)) * 100;
let totalFunctionPercent = (functionPercent / (i * 100)) * 100;
let totalStatementPercent = (statementPerment / (i * 100)) * 100;
let totalBranchesPercent = (branchesPercent / (i * 100)) * 100;
let totalPercentage =
totalLinesPercent +
totalFunctionPercent +
totalStatementPercent +
totalBranchesPercent;
let coverage = (totalPercentage / 4).toFixed(2);
coverage = Number(coverage);
if (Number.isNaN(coverage)) {
console.log('This build does\'t have any JS changes!');
coverage = 0;
} else {
console.log(`COVERAGE ${coverage}%`);
}
let html = `<html>
<head>
<style>
.red{
font-weight:bold;
color:red;
}
.green{
font-weight:bold;
color:green;
}
table
{
font-family: arial, sans-serif;
border-collapse: collapse;
}
td, th
{
border: 1px solid #dddddd;
padding: 8px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Title</th>
<th>FullName</th>
<th>Test Case Path</th>
</tr>
${unitTestReport.map(
t => `<tr>
<td>${t.title}</td>
<td>${t.fullName}</td>
<td>${t.filePath}</td>
</tr>`
)}
</table>
<br/>COVERAGE <span class="${
coverage < 60 ? 'red' : 'green'
}">${coverage}%</span> <br/> less than 60% consider failure
</body>
</html>
`;
if (!fs.existsSync('./unittest')) {
fs.mkdirSync('./unittest');
}
fs.writeFileSync('./unittest/index.html', html, 'utf8');
};
module.exports = result;