Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | import { execFile, type ExecFileException } from 'child_process'; import * as fs from 'fs'; import { FsTree } from 'nx/src/generators/tree'; import { findFiles } from '../utils/find-files.util'; import { logger } from '../utils/logger'; /** * Project root directory. */ const root = process.cwd(); interface ICoverageSummary { total: number; covered: number; skipped: number; pct: number | string; } interface ICoverageSummaryObj { lines: ICoverageSummary; statements: ICoverageSummary; functions: ICoverageSummary; branches: ICoverageSummary; branchesTrue: ICoverageSummary; } interface ICoverageSummaryJson { total: ICoverageSummaryObj; [key: string]: ICoverageSummaryObj; } const totalCoverage: ICoverageSummaryObj = { lines: { total: 0, covered: 0, skipped: 0, pct: 0 }, statements: { total: 0, covered: 0, skipped: 0, pct: 0 }, functions: { total: 0, covered: 0, skipped: 0, pct: 0 }, branches: { total: 0, covered: 0, skipped: 0, pct: 0 }, branchesTrue: { total: 0, covered: 0, skipped: 0, pct: 0 }, }; let readFiles = 0; const writeAverageStats = () => { const readmePath = `${root}/UNIT_COVERAGE.md`; const coverageSummary = `# Unit Coverage Stats ## Lines | Total | Covered | Skipped | PCT | | ----------------------------- | ------------------------------- | ------------------------------- | --------------------------- | | ${totalCoverage.lines.total} | ${totalCoverage.lines.covered} | ${totalCoverage.lines.skipped} | ${totalCoverage.lines.pct}% | ## Statements | Total | Covered | Skipped | PCT | | ---------------------------------- | ------------------------------------ | ------------------------------------ | -------------------------------- | | ${totalCoverage.statements.total} | ${totalCoverage.statements.covered} | ${totalCoverage.statements.skipped} | ${totalCoverage.statements.pct}% | ## Functions | Total | Covered | Skipped | PCT | | --------------------------------- | ----------------------------------- | ----------------------------------- | ------------------------------- | | ${totalCoverage.functions.total} | ${totalCoverage.functions.covered} | ${totalCoverage.functions.skipped} | ${totalCoverage.functions.pct}% | ## Branches | Total | Covered | Skipped | PCT | | -------------------------------- | ---------------------------------- | ---------------------------------- | ------------------------------ | | ${totalCoverage.branches.total} | ${totalCoverage.branches.covered} | ${totalCoverage.branches.skipped} | ${totalCoverage.branches.pct}% | `; fs.writeFile(readmePath, coverageSummary, (error: NodeJS.ErrnoException | null) => { if (error !== null) { logger.printError(error); } const command = 'yarn'; const args = ['prettier', readmePath, '--write']; execFile(command, args, {}, (err: ExecFileException | null) => { if (err !== null) { logger.printError(err); } }); }); }; /** * Projects count which coverage is more than 0. */ const projectsCount: Record<keyof ICoverageSummaryObj, number> = { branches: 0, functions: 0, lines: 0, statements: 0, branchesTrue: 0, }; const projectsCountKeys = Object.keys(projectsCount) as Array<keyof ICoverageSummaryObj>; const parseSummary = (summary: ICoverageSummaryObj, summaryKeys: Array<keyof ICoverageSummaryObj>) => { const zeroCoverage: Record<keyof ICoverageSummaryObj, number> = { branches: 0, functions: 0, lines: 0, statements: 0, branchesTrue: 0, }; for (const summaryKey of summaryKeys) { const summarySection = summary[summaryKey]; const summarySectionKeys = Object.keys(summarySection) as Array<keyof ICoverageSummary>; for (const summarySectionKey of summarySectionKeys) { const value = summarySection[summarySectionKey]; const currentValue = totalCoverage[summaryKey][summarySectionKey]; if (typeof value === 'number' && typeof currentValue === 'number') { if (summarySectionKey === 'pct' && value > 0) { zeroCoverage[summaryKey] = zeroCoverage[summaryKey] + 1; } const digits = 2; totalCoverage[summaryKey][summarySectionKey] = Number( (currentValue + (summarySectionKey === 'pct' ? value : value)).toFixed(digits), ); } } } for (const projectCountKey of projectsCountKeys) { if (zeroCoverage[projectCountKey] > 0) { projectsCount[projectCountKey] = projectsCount[projectCountKey] + 1; } } }; /** * Counts average PCT. */ const recalculateStats = () => { for (const projectsCountKey of projectsCountKeys) { const value = totalCoverage[projectsCountKey].pct as number; const digits = 2; totalCoverage[projectsCountKey].pct = Number((value / projectsCount[projectsCountKey]).toFixed(digits)); } }; (async (): Promise<void> => { const tree = new FsTree(process.cwd(), false); const files = findFiles(tree, 'dist/coverage', 'summary.json'); if (files.stderr) { throw new Error(files.stderr); } const fileList = files.stdout.split(' '); const readFileCallback = (error: NodeJS.ErrnoException | null, data?: Buffer) => { if (error !== null) { logger.printError(new Error('No coverage summary for the project')); } if (typeof data !== 'undefined') { const json: ICoverageSummaryJson = JSON.parse(data.toString()); logger.printSuccess(`Total:\n ${JSON.stringify(json.total)}`); const summary = json.total; const summaryKeys = Object.keys(summary) as Array<keyof ICoverageSummaryObj>; parseSummary(summary, summaryKeys); } readFiles += 1; if (readFiles === fileList.length) { recalculateStats(); writeAverageStats(); } }; for (const relativePath of fileList) { const absolutePath = `${process.cwd()}/${relativePath}`; fs.readFile(absolutePath, readFileCallback); } })().catch((error: Error) => { process.stderr.write(`Coverage stats generator error: ${error.toString()}\n`); process.exit(1); }); |