@@ -6,11 +6,13 @@ const {
6
6
ArrayPrototypeShift,
7
7
ArrayPrototypeUnshift,
8
8
hardenRegExp,
9
+ NumberPrototypeToFixed,
9
10
RegExpPrototypeSymbolSplit,
10
11
SafeMap,
11
12
StringPrototypeRepeat,
12
13
} = primordials ;
13
14
const assert = require ( 'assert' ) ;
15
+ const { relative } = require ( 'path' ) ;
14
16
const Transform = require ( 'internal/streams/transform' ) ;
15
17
const { inspectWithNoCustomRetry } = require ( 'internal/errors' ) ;
16
18
const { green, blue, red, white, gray } = require ( 'internal/util/colors' ) ;
@@ -23,12 +25,14 @@ const colors = {
23
25
'test:fail' : red ,
24
26
'test:pass' : green ,
25
27
'test:diagnostic' : blue ,
28
+ 'test:coverage' : blue ,
26
29
} ;
27
30
const symbols = {
28
31
'__proto__' : null ,
29
32
'test:fail' : '\u2716 ' ,
30
33
'test:pass' : '\u2714 ' ,
31
34
'test:diagnostic' : '\u2139 ' ,
35
+ 'test:coverage' : '\u2139 ' ,
32
36
'arrow:right' : '\u25B6 ' ,
33
37
} ;
34
38
class SpecReporter extends Transform {
@@ -59,6 +63,36 @@ class SpecReporter extends Transform {
59
63
) , `\n${ indent } ` ) ;
60
64
return `\n${ indent } ${ message } \n` ;
61
65
}
66
+ #reportCoverage( color , nesting , summary , symbol ) {
67
+ let report = `${ color } ${ this . #indent( nesting ) } ${ symbol } start of coverage report${ white } \n` ;
68
+ report += `${ color } ${ this . #indent( nesting ) } ${ symbol } file | line % | branch % | funcs % | uncovered lines${ white } \n` ;
69
+
70
+ for ( let i = 0 ; i < summary . files . length ; ++ i ) {
71
+ const {
72
+ path,
73
+ coveredLinePercent,
74
+ coveredBranchPercent,
75
+ coveredFunctionPercent,
76
+ uncoveredLineNumbers,
77
+ } = summary . files [ i ] ;
78
+ const relativePath = relative ( summary . workingDirectory , path ) ;
79
+ const lines = NumberPrototypeToFixed ( coveredLinePercent , 2 ) ;
80
+ const branches = NumberPrototypeToFixed ( coveredBranchPercent , 2 ) ;
81
+ const functions = NumberPrototypeToFixed ( coveredFunctionPercent , 2 ) ;
82
+ const uncovered = ArrayPrototypeJoin ( uncoveredLineNumbers , ', ' ) ;
83
+
84
+ report += `${ color } ${ this . #indent( nesting ) } ${ symbol } ${ relativePath } | ${ lines } | ${ branches } | ${ functions } | ${ uncovered } \n` ;
85
+ }
86
+
87
+ const { totals } = summary ;
88
+ report += `${ color } ${ this . #indent( nesting ) } ${ symbol } all files | ` +
89
+ `${ NumberPrototypeToFixed ( totals . coveredLinePercent , 2 ) } | ` +
90
+ `${ NumberPrototypeToFixed ( totals . coveredBranchPercent , 2 ) } | ` +
91
+ `${ NumberPrototypeToFixed ( totals . coveredFunctionPercent , 2 ) } |\n` ;
92
+
93
+ report += `${ color } ${ this . #indent( nesting ) } ${ symbol } end of coverage report\n` ;
94
+ return report ;
95
+ }
62
96
#handleEvent( { type, data } ) {
63
97
const color = colors [ type ] ?? white ;
64
98
const symbol = symbols [ type ] ?? ' ' ;
@@ -97,6 +131,8 @@ class SpecReporter extends Transform {
97
131
break ;
98
132
case 'test:diagnostic' :
99
133
return `${ color } ${ this . #indent( data . nesting ) } ${ symbol } ${ data . message } ${ white } \n` ;
134
+ case 'test:coverage' :
135
+ return this . #reportCoverage( color , data . nesting , data . summary , symbol ) ;
100
136
}
101
137
}
102
138
_transform ( { type, data } , encoding , callback ) {
0 commit comments