UNPKG

3.87 kBJavaScriptView Raw
1'use strict';
2
3var fs = require('fs');
4
5var unitTestReport = [];
6var result = function result(inp) {
7 var testPathPattern = process.argv[process.argv.length - 1];
8 if (testPathPattern.indexOf('--') != -1) {
9 testPathPattern = '';
10 } else {
11 testPathPattern = fs.realpathSync(process.cwd());
12 }
13 var testPathRegex = new RegExp(testPathPattern);
14 var testResults = inp.testResults;
15 var testFilesArr = [];
16 // for (var i = 0; i < testResults.length; i++) {
17 testResults.forEach(function (testResult, i) {
18 var filePath = testResult.testFilePath;
19 if (!testPathRegex.test(filePath)) {
20 return;
21 }
22 filePath = filePath.replace('.spec', '');
23 filePath = filePath.replace('/__tests__', '');
24 filePath = filePath.replace('/__test__', '');
25 var fileJson = {};
26 fileJson.testPath = testResult.testFilePath;
27 fileJson.sourcePath = filePath;
28 fileJson.data = testResult;
29 testFilesArr.push(fileJson);
30 testResult.testResults && testResult.testResults.filter(function (t) {
31 return t.status == 'failed';
32 }).forEach(function (t) {
33 unitTestReport.push({
34 title: t.title,
35 fullName: t.fullName,
36 filePath: testResult.testFilePath
37 });
38 });
39 });
40
41 var coverageSummary = fs.readFileSync('./coverage/coverage-summary.json').toString();
42 if (coverageSummary.indexOf('\\') != -1) {
43 coverageSummary = coverageSummary.replace(/\\/g, '\\\\');
44 }
45 var coverageJson = JSON.parse(coverageSummary);
46 var linesPercent = 0;
47 var functionPercent = 0;
48 var statementPerment = 0;
49 var branchesPercent = 0;
50 for (var i = 0; i < testFilesArr.length; i++) {
51 var curSourceFile = testFilesArr[i].sourcePath;
52 var coverageData = coverageJson[curSourceFile];
53 if (coverageData == undefined) {
54 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.');
55 continue;
56 }
57 linesPercent += coverageData.lines.pct;
58 functionPercent += coverageData.functions.pct;
59 statementPerment += coverageData.statements.pct;
60 branchesPercent += coverageData.branches.pct;
61 }
62 var totalLinesPercent = linesPercent / (i * 100) * 100;
63 var totalFunctionPercent = functionPercent / (i * 100) * 100;
64 var totalStatementPercent = statementPerment / (i * 100) * 100;
65 var totalBranchesPercent = branchesPercent / (i * 100) * 100;
66 var totalPercentage = totalLinesPercent + totalFunctionPercent + totalStatementPercent + totalBranchesPercent;
67 var coverage = (totalPercentage / 4).toFixed(2);
68 coverage = Number(coverage);
69
70 if (Number.isNaN(coverage)) {
71 console.log('This build does\'t have any JS changes!');
72 coverage = 0;
73 } else {
74 console.log('COVERAGE ' + coverage + '%');
75 }
76
77 var html = '<html>\n\t<head>\n\t<style>\n\t.red{\n\t\tfont-weight:bold;\n\t\tcolor:red;\n\t}\n\t.green{\n\t\tfont-weight:bold;\n\t\tcolor:green;\n\t}\n\ttable\n\t\t{\n font-family: arial, sans-serif;\n border-collapse: collapse;\n\t\t}\n\n\t\ttd, th\n\t\t{\n border: 1px solid #dddddd;\n padding: 8px;\n\t\t}\n\t</style>\n\t</head>\n\t\t<body>\n\t\t\t<table>\n\t\t\t<tr>\n\t\t\t\t<th>Title</th>\n\t\t\t\t<th>FullName</th>\n\t\t\t\t<th>Test Case Path</th>\n\t\t\t</tr>\n\t\t\t' + unitTestReport.map(function (t) {
78 return '<tr>\n\t\t\t\t\t<td>' + t.title + '</td>\n\t\t\t\t\t<td>' + t.fullName + '</td>\n\t\t\t\t\t<td>' + t.filePath + '</td>\n\t\t\t\t</tr>';
79 }) + '\n\t\t\t</table>\n\t\t\t<br/>COVERAGE <span class="' + (coverage < 60 ? 'red' : 'green') + '">' + coverage + '%</span> <br/> less than 60% consider failure\n\t\t</body>\n\t</html>\n\t\t';
80
81 if (!fs.existsSync('./unittest')) {
82 fs.mkdirSync('./unittest');
83 }
84 fs.writeFileSync('./unittest/index.html', html, 'utf8');
85};
86
87module.exports = result;
\No newline at end of file