3
3
const {
4
4
ArrayPrototypeJoin,
5
5
ArrayPrototypePop,
6
+ ArrayPrototypePush,
6
7
ArrayPrototypeShift,
7
8
ArrayPrototypeUnshift,
8
9
hardenRegExp,
@@ -36,6 +37,7 @@ class SpecReporter extends Transform {
36
37
#stack = [ ] ;
37
38
#reported = [ ] ;
38
39
#indentMemo = new SafeMap ( ) ;
40
+ #failedTests = [ ] ;
39
41
40
42
constructor ( ) {
41
43
super ( { writableObjectMode : true } ) ;
@@ -57,15 +59,29 @@ class SpecReporter extends Transform {
57
59
RegExpPrototypeSymbolSplit (
58
60
hardenRegExp ( / \r ? \n / ) ,
59
61
inspectWithNoCustomRetry ( err , inspectOptions ) ,
60
- ) , `\n${ indent } ` ) ;
61
- return `\n${ indent } ${ message } \n` ;
62
+ ) , indent !== undefined ? `\n${ indent } ` : '' ) ;
63
+ return `${ indent !== undefined ? ` \n${ indent } ` : '\n' } ${ message } \n` ;
62
64
}
63
- #handleEvent ( { type, data } ) {
65
+ #formatTestReport ( type , data , prefix = '' , indent = undefined , hasChildren = false , skippedSubtest = false , showFilePath = false ) {
64
66
let color = colors [ type ] ?? white ;
65
67
let symbol = symbols [ type ] ?? ' ' ;
66
-
68
+ const duration_ms = data . details ?. duration_ms ? ` ${ gray } (${ data . details . duration_ms } ms)${ white } ` : '' ;
69
+ const title = `${ showFilePath ? `${ data . file } ` : '' } ${ data . name } ${ duration_ms } ${ skippedSubtest ? ' # SKIP' : '' } ` ;
70
+ if ( hasChildren ) {
71
+ // If this test has had children - it was already reported, so slightly modify the output
72
+ return `${ prefix } ${ indent ?? '' } ${ color } ${ symbols [ 'arrow:right' ] } ${ white } ${ title } \n` ;
73
+ }
74
+ const error = this . #formatError( data . details ?. error , indent ) ;
75
+ if ( skippedSubtest ) {
76
+ color = gray ;
77
+ symbol = symbols [ 'hyphen:minus' ] ;
78
+ }
79
+ return `${ prefix } ${ indent ?? '' } ${ color } ${ symbol } ${ title } ${ error } ${ white } ` ;
80
+ }
81
+ #handleEvent( { type, data } ) {
67
82
switch ( type ) {
68
83
case 'test:fail' :
84
+ ArrayPrototypePush ( this . #failedTests, data ) ;
69
85
case 'test:pass' : {
70
86
const subtest = ArrayPrototypeShift ( this . #stack) ; // This is the matching `test:start` event
71
87
if ( subtest ) {
@@ -82,32 +98,40 @@ class SpecReporter extends Transform {
82
98
ArrayPrototypeUnshift ( this . #reported, msg ) ;
83
99
prefix += `${ this . #indent( msg . nesting ) } ${ symbols [ 'arrow:right' ] } ${ msg . name } \n` ;
84
100
}
85
- const skippedSubtest = subtest && data . skip && data . skip !== undefined ;
86
- const indent = this . #indent( data . nesting ) ;
87
- const duration_ms = data . details ?. duration_ms ? ` ${ gray } (${ data . details . duration_ms } ms)${ white } ` : '' ;
88
- const title = `${ data . name } ${ duration_ms } ${ skippedSubtest ? ' # SKIP' : '' } ` ;
101
+ let hasChildren = false ;
89
102
if ( this . #reported[ 0 ] && this . #reported[ 0 ] . nesting === data . nesting && this . #reported[ 0 ] . name === data . name ) {
90
- // If this test has had children - it was already reported, so slightly modify the output
91
103
ArrayPrototypeShift ( this . #reported) ;
92
- return `${ prefix } ${ indent } ${ color } ${ symbols [ 'arrow:right' ] } ${ white } ${ title } \n\n` ;
93
- }
94
- const error = this . #formatError( data . details ?. error , indent ) ;
95
- if ( skippedSubtest ) {
96
- color = gray ;
97
- symbol = symbols [ 'hyphen:minus' ] ;
104
+ hasChildren = true ;
98
105
}
99
- return `${ prefix } ${ indent } ${ color } ${ symbol } ${ title } ${ error } ${ white } \n` ;
106
+ const skippedSubtest = subtest && data . skip && data . skip !== undefined ;
107
+ const indent = this . #indent( data . nesting ) ;
108
+ return `${ this . #formatTestReport( type , data , prefix , indent , hasChildren , skippedSubtest ) } \n` ;
100
109
}
101
110
case 'test:start' :
102
111
ArrayPrototypeUnshift ( this . #stack, { __proto__ : null , data, type } ) ;
103
112
break ;
104
113
case 'test:diagnostic' :
105
- return `${ color } ${ this . #indent( data . nesting ) } ${ symbol } ${ data . message } ${ white } \n` ;
114
+ return `${ colors [ type ] } ${ this . #indent( data . nesting ) } ${ symbols [ type ] } ${ data . message } ${ white } \n` ;
106
115
}
107
116
}
108
117
_transform ( { type, data } , encoding , callback ) {
109
118
callback ( null , this . #handleEvent( { type, data } ) ) ;
110
119
}
120
+ _flush ( callback ) {
121
+ const results = [ `\n${ colors [ 'test:fail' ] } ${ symbols [ 'test:fail' ] } failing tests:${ white } \n` ] ;
122
+ for ( let i = 0 ; i < this . #failedTests. length ; i ++ ) {
123
+ ArrayPrototypePush ( results , this . #formatTestReport(
124
+ 'test:fail' ,
125
+ this . #failedTests[ i ] ,
126
+ '' ,
127
+ undefined ,
128
+ false ,
129
+ false ,
130
+ true ,
131
+ ) ) ;
132
+ }
133
+ callback ( null , ArrayPrototypeJoin ( results , '' ) ) ;
134
+ }
111
135
}
112
136
113
137
module . exports = SpecReporter ;
0 commit comments