@@ -31,6 +31,12 @@ const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//;
3131const kLineEndingRegex = / \r ? \n $ / u;
3232const kLineSplitRegex = / (?< = \r ? \n ) / u;
3333const kStatusRegex = / \/ \* n o d e : c o v e r a g e (?< status > e n a b l e | d i s a b l e ) \* \/ / ;
34+ const kDefaultMinimumThreshold = {
35+ __proto__ : null ,
36+ lines : 0 ,
37+ functions : 0 ,
38+ branches : 0 ,
39+ } ;
3440
3541class CoverageLine {
3642 #covered;
@@ -63,10 +69,11 @@ class CoverageLine {
6369}
6470
6571class TestCoverage {
66- constructor ( coverageDirectory , originalCoverageDirectory , workingDirectory ) {
72+ constructor ( coverageDirectory , originalCoverageDirectory , workingDirectory , minimumThreshold ) {
6773 this . coverageDirectory = coverageDirectory ;
6874 this . originalCoverageDirectory = originalCoverageDirectory ;
6975 this . workingDirectory = workingDirectory ;
76+ this . minimumThreshold = minimumThreshold || kDefaultMinimumThreshold ;
7077 }
7178
7279 summary ( ) {
@@ -260,6 +267,12 @@ class TestCoverage {
260267 ) ;
261268 coverageSummary . files . sort ( sortCoverageFiles ) ;
262269
270+ coverageSummary . threshold = {
271+ __proto__ : null ,
272+ lines : doesThresholdPass ( this . minimumThreshold . lines , coverageSummary . totals . coveredLinePercent ) ,
273+ functions : doesThresholdPass ( this . minimumThreshold . functions , coverageSummary . totals . coveredFunctionPercent ) ,
274+ branches : doesThresholdPass ( this . minimumThreshold . branches , coverageSummary . totals . coveredBranchPercent ) ,
275+ } ;
263276 return coverageSummary ;
264277 }
265278
@@ -299,7 +312,7 @@ function sortCoverageFiles(a, b) {
299312 return StringPrototypeLocaleCompare ( a . path , b . path ) ;
300313}
301314
302- function setupCoverage ( ) {
315+ function setupCoverage ( minimumThreshold = null ) {
303316 let originalCoverageDirectory = process . env . NODE_V8_COVERAGE ;
304317 const cwd = process . cwd ( ) ;
305318
@@ -323,7 +336,7 @@ function setupCoverage() {
323336 // child processes.
324337 process . env . NODE_V8_COVERAGE = coverageDirectory ;
325338
326- return new TestCoverage ( coverageDirectory , originalCoverageDirectory , cwd ) ;
339+ return new TestCoverage ( coverageDirectory , originalCoverageDirectory , cwd , minimumThreshold ) ;
327340}
328341
329342function mapRangeToLines ( range , lines ) {
@@ -538,4 +551,8 @@ function doesRangeContainOtherRange(range, otherRange) {
538551 range . endOffset >= otherRange . endOffset ;
539552}
540553
554+ function doesThresholdPass ( compareValue , actualValue ) {
555+ return compareValue ? actualValue >= compareValue : true ;
556+ }
557+
541558module . exports = { setupCoverage, TestCoverage } ;
0 commit comments