1
- var format = require ( 'ansi-escape' )
1
+ var ansi = require ( 'ansi-escape' )
2
+ var noAnsi = require ( './no-ansi' ) ;
2
3
var symbols = require ( 'figures' )
3
4
var prettyMs = require ( 'pretty-ms' )
4
5
var LF = '\n'
5
6
6
7
var summarize = require ( './summarize' )
7
8
8
- module . exports = function ( ) {
9
+ module . exports = function ( opts ) {
10
+ opts = opts || { }
11
+ opts . ansi = typeof opts . ansi !== 'undefined' ? opts . ansi : true
12
+ opts . progress = typeof opts . progress !== 'undefined' ? opts . progress : true
13
+ opts . markdown = typeof opts . markdown !== 'undefined' ? opts . markdown : false
14
+
15
+ var format = opts . ansi ? ansi : noAnsi
16
+ var splitter = opts . markdown ? mdSplitter : barSplitter
17
+
18
+ var INDENT = opts . markdown ? repeat ( ' ' , 4 ) : ''
19
+ var LIST = opts . markdown ? '- ' : ''
20
+
9
21
var summary = summarize ( )
10
22
var output = summary . output
11
23
12
24
output . push ( LF + splitter ( ' Tests ' ) )
13
25
14
26
summary . on ( 'test.start' , function ( test ) {
15
27
Object . defineProperty ( test , 'title' , { get : getTitle } )
16
- output . push ( LF + format . cha . eraseLine . escape ( '# ' + test . title ) )
17
28
} )
18
29
19
30
summary . on ( 'test.end' , function ( test ) {
20
31
if ( test . fail ) {
21
- output . push ( format . cha . red . eraseLine . escape ( symbols . cross + ' ' + test . title ) )
32
+ output . push ( format . cha . red . eraseLine . escape ( INDENT + symbols . cross + ' ' + test . title ) )
22
33
} else {
23
- output . push ( format . cha . green . eraseLine . escape ( symbols . tick + ' ' + test . title ) )
34
+ output . push ( format . cha . green . eraseLine . escape ( INDENT + symbols . tick + ' ' + test . title ) )
24
35
}
25
36
} )
26
37
27
- summary . on ( 'test.pass' , function ( test ) {
28
- output . push ( format . cha . eraseLine . escape ( '# ' + test . title ) )
29
- } )
38
+ if ( opts . progress ) {
39
+ summary . on ( 'test.start' , function ( test ) {
40
+ output . push ( LF + format . cha . eraseLine . escape ( INDENT + '# ' + test . title ) )
41
+ } )
30
42
31
- summary . on ( 'test.fail' , function ( test ) {
32
- output . push ( format . cha . eraseLine . escape ( '# ' + test . title ) )
33
- } )
43
+ summary . on ( 'test.pass' , function ( test ) {
44
+ output . push ( format . cha . eraseLine . escape ( INDENT + '# ' + test . title ) )
45
+ } )
46
+
47
+ summary . on ( 'test.fail' , function ( test ) {
48
+ output . push ( format . cha . eraseLine . escape ( INDENT + '# ' + test . title ) )
49
+ } )
50
+ }
34
51
35
52
summary . on ( 'summary' , function ( sum , fails , comments ) {
36
53
output . push ( formatSummary ( sum ) )
@@ -46,86 +63,94 @@ module.exports = function () {
46
63
} )
47
64
48
65
return summary
49
- }
50
66
51
- function getTitle ( ) {
52
- return this . name + ' [' +
53
- 'pass: ' + this . pass + ', fail: ' + this . fail +
54
- ( this . duration ? ', duration: ' + prettyMs ( this . duration ) : '' ) +
55
- ']'
56
- }
67
+ function getTitle ( ) {
68
+ return this . name + ' [' +
69
+ 'pass: ' + this . pass + ', fail: ' + this . fail +
70
+ ( this . duration ? ', duration: ' + prettyMs ( this . duration ) : '' ) +
71
+ ']'
72
+ }
57
73
58
- function formatSummary ( res ) {
59
- var output = [ LF ]
60
- output . push ( splitter ( ' Summary ' ) )
61
- output . push ( format . cyan . escape ( 'duration: ' + prettyMs ( res . duration ) ) )
62
- output . push ( format . cyan . escape ( 'assertions: ' + res . assertions ) )
63
- if ( res . pass ) {
64
- output . push ( format . green . escape ( 'pass: ' + res . pass ) )
65
- } else {
66
- output . push ( format . cyan . escape ( 'pass: ' + res . pass ) )
74
+ function formatSummary ( res ) {
75
+ var output = [ LF ]
76
+ output . push ( splitter ( ' Summary ' ) )
77
+ output . push ( format . cyan . escape ( LIST + 'duration: ' + prettyMs ( res . duration ) ) )
78
+ output . push ( format . cyan . escape ( LIST + 'assertions: ' + res . assertions ) )
79
+ if ( res . pass ) {
80
+ output . push ( format . green . escape ( LIST + 'pass: ' + res . pass ) )
81
+ } else {
82
+ output . push ( format . cyan . escape ( LIST + 'pass: ' + res . pass ) )
83
+ }
84
+ if ( res . fail ) {
85
+ output . push ( format . red . escape ( LIST + 'fail: ' + res . fail ) )
86
+ } else {
87
+ output . push ( format . cyan . escape ( LIST + 'fail: ' + res . fail ) )
88
+ }
89
+ return output . join ( LF )
67
90
}
68
- if ( res . fail ) {
69
- output . push ( format . red . escape ( 'fail: ' + res . fail ) )
70
- } else {
71
- output . push ( format . cyan . escape ( 'fail: ' + res . fail ) )
91
+
92
+ function formatComment ( comments ) {
93
+ var output = [ LF ]
94
+ output . push ( splitter ( ' Comments ' ) )
95
+ output . push ( Object . keys ( comments ) . map ( function ( name ) {
96
+ return format . cyan . underline . escape ( name ) + LF + comments [ name ] . join ( LF )
97
+ } ) . join ( LF + LF ) )
98
+ return output . join ( LF )
72
99
}
73
- return output . join ( LF )
74
- }
75
100
76
- function formatComment ( comments ) {
77
- var output = [ LF ]
78
- output . push ( splitter ( ' Comments ' ) )
79
- output . push ( Object . keys ( comments ) . map ( function ( name ) {
80
- return format . cyan . underline . escape ( name ) + LF + comments [ name ] . join ( LF )
81
- } ) . join ( LF + LF ) )
82
- return output . join ( LF )
83
- }
101
+ function barSplitter ( s ) {
102
+ var len = s && s . length || 0
103
+ var max = 80
104
+ var left = max - len >> 1
105
+ return format . yellow . escape (
106
+ repeat ( '-' , left ) + ( s || '' ) + repeat ( '-' , max - len - left )
107
+ )
108
+ }
84
109
85
- function splitter ( s ) {
86
- var len = s && s . length || 0
87
- var max = 80
88
- var left = max - len >> 1
89
- return format . yellow . escape (
90
- repeat ( '-' , left ) + ( s || '' ) + repeat ( '-' , max - len - left )
91
- )
92
- }
110
+ function mdSplitter ( s , left ) {
111
+ left = arguments . length > 1 ? left : 1
112
+ return format . yellow . escape (
113
+ repeat ( '#' , left ) + ( s || '' ) + LF
114
+ )
115
+ }
93
116
94
- function repeat ( str , n ) {
95
- if ( str . repeat ) {
96
- return str . repeat ( n )
117
+ function repeat ( str , n ) {
118
+ if ( str . repeat ) {
119
+ return str . repeat ( n )
120
+ }
121
+ return ( new Array ( n + 1 ) ) . join ( str )
97
122
}
98
- return ( new Array ( n + 1 ) ) . join ( str )
99
- }
100
123
101
- function formatFail ( fail ) {
102
- var output = [ LF ]
103
- output . push ( splitter ( ' Fails ' ) )
104
- output . push (
105
- Object . keys ( fail ) . map ( function ( name ) {
106
- var res = [ format . cyan . underline . escape ( '# ' + name ) ]
107
- fail [ name ] . forEach ( function ( assertion ) {
108
- res . push ( format . red . escape ( ' ' + symbols . cross + ' ' + assertion . name ) )
109
- res . push ( prettifyError ( assertion ) )
110
- } )
111
- return res . join ( LF )
112
- } ) . join ( LF + LF )
113
- )
114
-
115
- return output . join ( LF )
116
- }
124
+ function formatFail ( fail ) {
125
+ var output = [ LF ]
126
+ output . push ( splitter ( ' Fails ' ) )
127
+ output . push (
128
+ Object . keys ( fail ) . map ( function ( name ) {
129
+ var res = [ format . cyan . underline . escape ( '# ' + name ) ]
130
+ fail [ name ] . forEach ( function ( assertion ) {
131
+ res . push ( format . red . escape ( INDENT + ' ' + symbols . cross + ' ' + assertion . name ) )
132
+ res . push ( prettifyError ( assertion ) )
133
+ } )
134
+ return res . join ( LF )
135
+ } ) . join ( LF + LF )
136
+ )
137
+
138
+ return output . join ( LF )
139
+ }
117
140
118
- function prettifyError ( assertion ) {
119
- var rawError = assertion . error . raw
120
- var ret = rawError . split ( LF )
121
- var stack = assertion . error . stack
122
- if ( stack ) {
123
- stack = stack . split ( LF )
124
- var padding = repeat ( ' ' , ret [ ret . length - 1 ] . length )
125
- ret = ret . concat ( stack . map ( function ( s ) {
126
- return padding + s
127
- } ) )
141
+ function prettifyError ( assertion ) {
142
+ var rawError = assertion . error . raw
143
+ var ret = rawError . split ( LF ) . map ( function ( s ) {
144
+ return INDENT + s
145
+ } )
146
+ var stack = assertion . error . stack
147
+ if ( stack ) {
148
+ stack = stack . split ( LF )
149
+ var padding = repeat ( ' ' , ret [ ret . length - 1 ] . length )
150
+ ret = ret . concat ( stack . map ( function ( s ) {
151
+ return padding + s
152
+ } ) )
153
+ }
154
+ return format . cyan . escape ( ret . join ( LF ) )
128
155
}
129
- return format . cyan . escape ( ret . join ( LF ) )
130
156
}
131
-
0 commit comments