1
1
'use strict' ;
2
-
3
2
const {
4
3
ArrayPrototypeJoin,
5
4
ArrayPrototypePop,
6
5
ArrayPrototypePush,
7
6
ArrayPrototypeShift,
8
7
ArrayPrototypeUnshift,
9
- RegExpPrototypeSymbolSplit,
10
- SafeMap,
11
- StringPrototypeRepeat,
12
- hardenRegExp,
13
8
} = primordials ;
14
9
const assert = require ( 'assert' ) ;
15
10
const Transform = require ( 'internal/streams/transform' ) ;
16
- const { inspectWithNoCustomRetry } = require ( 'internal/errors' ) ;
17
11
const colors = require ( 'internal/util/colors' ) ;
18
12
const { kSubtestsFailed } = require ( 'internal/test_runner/test' ) ;
19
13
const { getCoverageReport } = require ( 'internal/test_runner/utils' ) ;
20
14
const { relative } = require ( 'path' ) ;
15
+ const {
16
+ formatTestReport,
17
+ indent,
18
+ reporterColorMap,
19
+ reporterUnicodeSymbolMap,
20
+ } = require ( 'internal/test_runner/reporter/utils' ) ;
21
21
22
- const symbols = {
23
- '__proto__' : null ,
24
- 'test:fail' : '\u2716 ' ,
25
- 'test:pass' : '\u2714 ' ,
26
- 'test:diagnostic' : '\u2139 ' ,
27
- 'test:coverage' : '\u2139 ' ,
28
- 'arrow:right' : '\u25B6 ' ,
29
- 'hyphen:minus' : '\uFE63 ' ,
30
- } ;
31
22
class SpecReporter extends Transform {
32
23
#stack = [ ] ;
33
24
#reported = [ ] ;
34
- #indentMemo = new SafeMap ( ) ;
35
25
#failedTests = [ ] ;
36
26
#cwd = process . cwd ( ) ;
37
- #inspectOptions;
38
- #colors;
39
27
40
28
constructor ( ) {
41
29
super ( { __proto__ : null , writableObjectMode : true } ) ;
42
30
colors . refresh ( ) ;
43
- this . #inspectOptions = { __proto__ : null , colors : colors . shouldColorize ( process . stdout ) , breakLength : Infinity } ;
44
- this . #colors = {
45
- '__proto__' : null ,
46
- 'test:fail' : colors . red ,
47
- 'test:pass' : colors . green ,
48
- 'test:diagnostic' : colors . blue ,
49
- } ;
50
31
}
51
32
52
- #indent( nesting ) {
53
- let value = this . #indentMemo. get ( nesting ) ;
54
- if ( value === undefined ) {
55
- value = StringPrototypeRepeat ( ' ' , nesting ) ;
56
- this . #indentMemo. set ( nesting , value ) ;
57
- }
58
-
59
- return value ;
60
- }
61
- #formatError( error , indent ) {
62
- if ( ! error ) return '' ;
63
- const err = error . code === 'ERR_TEST_FAILURE' ? error . cause : error ;
64
- const message = ArrayPrototypeJoin (
65
- RegExpPrototypeSymbolSplit (
66
- hardenRegExp ( / \r ? \n / ) ,
67
- inspectWithNoCustomRetry ( err , this . #inspectOptions) ,
68
- ) , `\n${ indent } ` ) ;
69
- return `\n${ indent } ${ message } \n` ;
70
- }
71
- #formatTestReport( type , data , prefix = '' , indent = '' , hasChildren = false ) {
72
- let color = this . #colors[ type ] ?? colors . white ;
73
- let symbol = symbols [ type ] ?? ' ' ;
74
- const { skip, todo } = data ;
75
- const duration_ms = data . details ?. duration_ms ? ` ${ colors . gray } (${ data . details . duration_ms } ms)${ colors . white } ` : '' ;
76
- let title = `${ data . name } ${ duration_ms } ` ;
77
-
78
- if ( skip !== undefined ) {
79
- title += ` # ${ typeof skip === 'string' && skip . length ? skip : 'SKIP' } ` ;
80
- } else if ( todo !== undefined ) {
81
- title += ` # ${ typeof todo === 'string' && todo . length ? todo : 'TODO' } ` ;
82
- }
83
- const error = this . #formatError( data . details ?. error , indent ) ;
84
- if ( hasChildren ) {
85
- // If this test has had children - it was already reported, so slightly modify the output
86
- const err = ! error || data . details ?. error ?. failureType === 'subtestsFailed' ? '' : `\n${ error } ` ;
87
- return `${ prefix } ${ indent } ${ color } ${ symbols [ 'arrow:right' ] } ${ colors . white } ${ title } ${ err } ` ;
88
- }
89
- if ( skip !== undefined ) {
90
- color = colors . gray ;
91
- symbol = symbols [ 'hyphen:minus' ] ;
92
- }
93
- return `${ prefix } ${ indent } ${ color } ${ symbol } ${ title } ${ colors . white } ${ error } ` ;
94
- }
95
33
#handleTestReportEvent( type , data ) {
96
34
const subtest = ArrayPrototypeShift ( this . #stack) ; // This is the matching `test:start` event
97
35
if ( subtest ) {
@@ -106,15 +44,15 @@ class SpecReporter extends Transform {
106
44
assert ( parent . type === 'test:start' ) ;
107
45
const msg = parent . data ;
108
46
ArrayPrototypeUnshift ( this . #reported, msg ) ;
109
- prefix += `${ this . # indent( msg . nesting ) } ${ symbols [ 'arrow:right' ] } ${ msg . name } \n` ;
47
+ prefix += `${ indent ( msg . nesting ) } ${ reporterUnicodeSymbolMap [ 'arrow:right' ] } ${ msg . name } \n` ;
110
48
}
111
49
let hasChildren = false ;
112
50
if ( this . #reported[ 0 ] && this . #reported[ 0 ] . nesting === data . nesting && this . #reported[ 0 ] . name === data . name ) {
113
51
ArrayPrototypeShift ( this . #reported) ;
114
52
hasChildren = true ;
115
53
}
116
- const indent = this . # indent( data . nesting ) ;
117
- return `${ this . # formatTestReport( type , data , prefix , indent , hasChildren ) } \n` ;
54
+ const indentation = indent ( data . nesting ) ;
55
+ return `${ formatTestReport ( type , data , prefix , indentation , hasChildren ) } \n` ;
118
56
}
119
57
#handleEvent( { type, data } ) {
120
58
switch ( type ) {
@@ -132,9 +70,10 @@ class SpecReporter extends Transform {
132
70
case 'test:stdout' :
133
71
return data . message ;
134
72
case 'test:diagnostic' :
135
- return `${ this . #colors [ type ] } ${ this . # indent( data . nesting ) } ${ symbols [ type ] } ${ data . message } ${ colors . white } \n` ;
73
+ return `${ reporterColorMap [ type ] } ${ indent ( data . nesting ) } ${ reporterUnicodeSymbolMap [ type ] } ${ data . message } ${ colors . white } \n` ;
136
74
case 'test:coverage' :
137
- return getCoverageReport ( this . #indent( data . nesting ) , data . summary , symbols [ 'test:coverage' ] , colors . blue , true ) ;
75
+ return getCoverageReport ( indent ( data . nesting ) , data . summary ,
76
+ reporterUnicodeSymbolMap [ 'test:coverage' ] , colors . blue , true ) ;
138
77
}
139
78
}
140
79
_transform ( { type, data } , encoding , callback ) {
@@ -145,10 +84,10 @@ class SpecReporter extends Transform {
145
84
callback ( null , '' ) ;
146
85
return ;
147
86
}
148
- const results = [ `\n${ this . #colors [ 'test:fail' ] } ${ symbols [ 'test:fail' ] } failing tests:${ colors . white } \n` ] ;
87
+ const results = [ `\n${ reporterColorMap [ 'test:fail' ] } ${ reporterUnicodeSymbolMap [ 'test:fail' ] } failing tests:${ colors . white } \n` ] ;
149
88
for ( let i = 0 ; i < this . #failedTests. length ; i ++ ) {
150
89
const test = this . #failedTests[ i ] ;
151
- const formattedErr = this . # formatTestReport( 'test:fail' , test ) ;
90
+ const formattedErr = formatTestReport ( 'test:fail' , test ) ;
152
91
153
92
if ( test . file ) {
154
93
const relPath = relative ( this . #cwd, test . file ) ;
0 commit comments