Skip to content

Commit

Permalink
update README for onEnd hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jan 5, 2024
1 parent 8599a52 commit 14eefad
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 |
| :--------------| :------ | :------ | :---------------------- |
Expand Down Expand Up @@ -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
}
}
});
Expand Down
10 changes: 9 additions & 1 deletion lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 22 additions & 0 deletions test/test-v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};

Expand Down

0 comments on commit 14eefad

Please sign in to comment.