diff --git a/README.md b/README.md index d9d4bd0b..acd53c9f 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ * [Default Options](#default-options) * [Available Reports](#available-reports) * [Multiprocessing Support](#multiprocessing-support) +* [onEnd Hook](#onend-hook) * [Compare Reports](#compare-reports) * [Compare Workflows](#compare-workflows) * [Collecting Istanbul Coverage Data](#collecting-istanbul-coverage-data) @@ -129,6 +130,37 @@ const coverageResults = await coverageReport.generate(); console.log(coverageResults.summary); ``` +## onEnd Hook +For example, checking thresholds: +```js +const EC = require('eight-colors'); +const coverageOptions = { + name: 'My Coverage Report', + outputDir: './coverage-reports', + onEnd: (coverageResults) => { + const thresholds = { + bytes: 80, + lines: 60 + }; + console.log('check thresholds ...', thresholds); + const errors = []; + const { summary } = coverageResults; + Object.keys(thresholds).forEach((k) => { + const pct = summary[k].pct; + if (pct < thresholds[k]) { + errors.push(`Coverage threshold for ${k} (${pct} %) not met: ${thresholds[k]} %`); + } + }); + if (errors.length) { + const errMsg = errors.join('\n'); + console.log(EC.red(errMsg)); + // throw new Error(errMsg); + // process.exit(1); + } + } +} +``` + ## Compare Reports | | Istanbul | V8 | V8 to Istanbul | | :--------------| :------ | :------ | :---------------------- | @@ -245,13 +277,15 @@ And also there are some problems about counting the functions and branches: functions.forEach(block => { block.ranges.forEach((range, i) => { if (block.isBlockCoverage) { - // v8-to-istanbul: count range as branch here. + // v8-to-istanbul: new CovBranch() // Problem: not every block is branch, and the first block is actually function. if (block.functionName && i === 0) { - // v8-to-istanbul: count range as function here. + // v8-to-istanbul: new CovFunction() + // Problem: no anonymous function } } else if (block.functionName) { - // v8-to-istanbul: count range as function here. + // v8-to-istanbul: new CovFunction() + // Problem: no anonymous function } } }); diff --git a/lib/converter/converter.js b/lib/converter/converter.js index c789b413..02742ae6 100644 --- a/lib/converter/converter.js +++ b/lib/converter/converter.js @@ -5,9 +5,17 @@ * @false means that there is a single range and its count is the number of times the function was called. * @true means that the ranges form a tree of blocks representing how many times each statement or expression inside was executed. * It detects skipped or repeated statements. The root range counts the number of function calls. - * * @functionName can be an empty string. This is common for the FunctionCov representing the whole module. */ +// https://github.com/bcoe/v8-coverage +/** +* @ranges is always non-empty. The first range is called the "root range". +* @isBlockCoverage indicates if the function has block coverage information. + If this is false, it usually means that the functions was never called. + It seems to be equivalent to ranges.length === 1 && ranges[0].count === 0. +* @functionName can be an empty string. This is common for the FunctionCov representing the whole module. +*/ + // if you have a line of code that says `var x= 10; console.log(x);` that's one line and 2 statements. const path = require('path'); diff --git a/test/test-v8.js b/test/test-v8.js index fce90c72..51f953e2 100644 --- a/test/test-v8.js +++ b/test/test-v8.js @@ -24,6 +24,28 @@ const coverageOptions = { // v8Ignore: false, + onEnd: (coverageResults) => { + const thresholds = { + bytes: 80, + lines: 60 + }; + console.log('check thresholds ...', thresholds); + const errors = []; + const { summary } = coverageResults; + Object.keys(thresholds).forEach((k) => { + const pct = summary[k].pct; + if (pct < thresholds[k]) { + errors.push(`Coverage threshold for ${k} (${pct} %) not met: ${thresholds[k]} %`); + } + }); + if (errors.length) { + const errMsg = errors.join('\n'); + console.log(EC.red(errMsg)); + // throw new Error(errMsg); + // process.exit(1); + } + }, + outputDir: './docs/v8' };