11'use strict' ;
2-
32const {
43 ArrayPrototypeJoin,
54 ArrayPrototypePop,
65 ArrayPrototypePush,
76 ArrayPrototypeShift,
87 ArrayPrototypeUnshift,
9- RegExpPrototypeSymbolSplit,
10- SafeMap,
11- StringPrototypeRepeat,
12- hardenRegExp,
138} = primordials ;
149const assert = require ( 'assert' ) ;
1510const Transform = require ( 'internal/streams/transform' ) ;
16- const { inspectWithNoCustomRetry } = require ( 'internal/errors' ) ;
1711const colors = require ( 'internal/util/colors' ) ;
1812const { kSubtestsFailed } = require ( 'internal/test_runner/test' ) ;
1913const { getCoverageReport } = require ( 'internal/test_runner/utils' ) ;
2014const { relative } = require ( 'path' ) ;
15+ const {
16+ formatTestReport,
17+ indent,
18+ reporterColorMap,
19+ reporterUnicodeSymbolMap,
20+ } = require ( 'internal/test_runner/reporter/utils' ) ;
2121
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- } ;
3122class SpecReporter extends Transform {
3223 #stack = [ ] ;
3324 #reported = [ ] ;
34- #indentMemo = new SafeMap ( ) ;
3525 #failedTests = [ ] ;
3626 #cwd = process . cwd ( ) ;
37- #inspectOptions;
38- #colors;
3927
4028 constructor ( ) {
4129 super ( { __proto__ : null , writableObjectMode : true } ) ;
4230 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- } ;
5031 }
5132
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- }
9533 #handleTestReportEvent( type , data ) {
9634 const subtest = ArrayPrototypeShift ( this . #stack) ; // This is the matching `test:start` event
9735 if ( subtest ) {
@@ -106,15 +44,15 @@ class SpecReporter extends Transform {
10644 assert ( parent . type === 'test:start' ) ;
10745 const msg = parent . data ;
10846 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` ;
11048 }
11149 let hasChildren = false ;
11250 if ( this . #reported[ 0 ] && this . #reported[ 0 ] . nesting === data . nesting && this . #reported[ 0 ] . name === data . name ) {
11351 ArrayPrototypeShift ( this . #reported) ;
11452 hasChildren = true ;
11553 }
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` ;
11856 }
11957 #handleEvent( { type, data } ) {
12058 switch ( type ) {
@@ -132,9 +70,10 @@ class SpecReporter extends Transform {
13270 case 'test:stdout' :
13371 return data . message ;
13472 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` ;
13674 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 ) ;
13877 }
13978 }
14079 _transform ( { type, data } , encoding , callback ) {
@@ -145,10 +84,10 @@ class SpecReporter extends Transform {
14584 callback ( null , '' ) ;
14685 return ;
14786 }
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` ] ;
14988 for ( let i = 0 ; i < this . #failedTests. length ; i ++ ) {
15089 const test = this . #failedTests[ i ] ;
151- const formattedErr = this . # formatTestReport( 'test:fail' , test ) ;
90+ const formattedErr = formatTestReport ( 'test:fail' , test ) ;
15291
15392 if ( test . file ) {
15493 const relPath = relative ( this . #cwd, test . file ) ;
0 commit comments