11var Table = require ( 'cli-table' ) ;
22
33var data ,
4- log = console . log ;
4+ log = console . log ,
5+ fileColWidth = 50 ;
56
67data = {
78 assertions : [ ] ,
89 tests : [ ] ,
9- summaries : [ ]
10+ summaries : [ ] ,
11+ coverages : [ ]
1012} ;
1113
12- exports . assertion = function ( d ) {
14+ exports . add = function ( t , d ) {
1315 if ( d ) {
14- data . assertions . push ( d ) ;
16+ data [ t ] . push ( d ) ;
1517 }
16-
17- return data . assertions ;
18- } ;
19-
20- exports . test = function ( d ) {
21- if ( d ) {
22- data . tests . push ( d ) ;
23- }
24-
25- return data . tests ;
26- } ;
27-
28- exports . summary = function ( d ) {
29- if ( d ) {
30- data . summaries . push ( d ) ;
31- }
32-
33- return data . summaries ;
18+ return data [ t ] ;
3419} ;
3520
3621/**
3722 * Get global tests stats in unified format
3823 */
3924exports . stats = function ( ) {
4025 var stats = {
41- files : 0 ,
42- assertions : 0 ,
43- failed : 0 ,
44- passed : 0 ,
45- runtime : 0
46- } ;
26+ files : 0 ,
27+ assertions : 0 ,
28+ failed : 0 ,
29+ passed : 0 ,
30+ runtime : 0
31+ } ;
4732
4833 data . summaries . forEach ( function ( file ) {
4934 stats . files ++ ;
@@ -55,6 +40,26 @@ exports.stats = function() {
5540
5641 stats . tests = data . tests . length ;
5742
43+ stats . coverage = {
44+ files : 0 ,
45+ statements : { covered : 0 , total : 0 } ,
46+ branches : { covered : 0 , total : 0 } ,
47+ functions : { covered : 0 , total : 0 } ,
48+ lines : { covered : 0 , total : 0 }
49+ } ;
50+
51+ data . coverages . forEach ( function ( file ) {
52+ stats . coverage . files ++ ;
53+ stats . coverage . statements . covered += file . statements . covered ;
54+ stats . coverage . statements . total += file . statements . total ;
55+ stats . coverage . branches . covered += file . branches . covered ;
56+ stats . coverage . branches . total += file . branches . total ;
57+ stats . coverage . functions . covered += file . functions . covered ;
58+ stats . coverage . functions . total += file . functions . total ;
59+ stats . coverage . lines . covered += file . lines . covered ;
60+ stats . coverage . lines . total += file . lines . total ;
61+ } ) ;
62+
5863 return stats ;
5964} ;
6065
@@ -65,7 +70,8 @@ exports.reset = function() {
6570 data = {
6671 assertions : [ ] ,
6772 tests : [ ] ,
68- summaries : [ ]
73+ summaries : [ ] ,
74+ coverages : [ ]
6975 } ;
7076} ;
7177
@@ -159,23 +165,22 @@ print.tests = function() {
159165 log ( '\nTests:\n' + table . toString ( ) ) ;
160166} ;
161167
162- print . summary = function ( ) {
163- var table , fileColWidth = 50 ;
168+ // truncate file name
169+ function truncfile ( code ) {
170+ if ( code && code . length > fileColWidth ) {
171+ code = '...' + code . slice ( code . length - fileColWidth + 3 ) ;
172+ }
173+ return code ;
174+ }
164175
165- table = new Table ( {
176+ print . summary = function ( ) {
177+ var table = new Table ( {
166178 head : [ 'File' , 'Failed' , 'Passed' , 'Total' , 'Runtime' ] ,
167179 colWidths : [ fileColWidth + 2 , 10 , 10 , 10 , 10 ]
168180 } ) ;
169181
170182 data . summaries . forEach ( function ( data ) {
171- var code = data . code ;
172-
173- // truncate file name
174- if ( code . length > fileColWidth ) {
175- code = '...' + code . slice ( code . length - fileColWidth + 3 ) ;
176- }
177-
178- table . push ( [ code , data . failed , data . passed , data . total , data . runtime ] ) ;
183+ table . push ( [ truncfile ( data . code ) , data . failed , data . passed , data . total , data . runtime ] ) ;
179184 } ) ;
180185
181186 log ( '\nSummary:\n' + table . toString ( ) ) ;
@@ -195,3 +200,43 @@ print.globalSummary = function() {
195200
196201 log ( '\nGlobal summary:\n' + table . toString ( ) ) ;
197202} ;
203+
204+ function getMet ( metric ) {
205+ function percent ( covered , total ) {
206+ var tmp ;
207+ if ( total > 0 ) {
208+ tmp = 1000 * 100 * covered / total + 5 ;
209+ return Math . floor ( tmp / 10 ) / 100 ;
210+ } else {
211+ return 100.00 ;
212+ }
213+ }
214+ if ( ! metric . pct ) metric . pct = percent ( metric . covered , metric . total ) ;
215+ return metric . pct + '% (' + metric . covered + '/' + metric . total + ')' ;
216+ }
217+
218+ print . coverage = function ( ) {
219+ var table = new Table ( {
220+ head : [ 'File' , 'Statements' , 'Branches' , 'Functions' , 'Lines' ] ,
221+ colWidths : [ fileColWidth + 2 , 14 , 14 , 14 , 14 ]
222+ } ) ;
223+
224+ data . coverages . forEach ( function ( data ) {
225+ table . push ( [ truncfile ( data . code ) , getMet ( data . statements ) , getMet ( data . branches ) , getMet ( data . functions ) , getMet ( data . lines ) ] ) ;
226+ } ) ;
227+
228+ log ( '\nCoverage:\n' + table . toString ( ) ) ;
229+ } ;
230+
231+ print . globalCoverage = function ( ) {
232+ var data = exports . stats ( ) . coverage ;
233+
234+ var table = new Table ( {
235+ head : [ 'Files' , 'Statements' , 'Branches' , 'Functions' , 'Lines' ] ,
236+ colWidths : [ 8 , 14 , 14 , 14 , 14 ]
237+ } ) ;
238+
239+ table . push ( [ data . files , getMet ( data . statements ) , getMet ( data . branches ) , getMet ( data . functions ) , getMet ( data . lines ) ] ) ;
240+
241+ log ( '\nGlobal coverage:\n' + table . toString ( ) ) ;
242+ } ;
0 commit comments