1+ /**
2+ * Custom Jest reporter that provides verbose test output with colors
3+ * while suppressing console logs for passing tests. Only shows console
4+ * output (like console.error) for failed tests.
5+ */
6+
7+ const { DefaultReporter } = require ( '@jest/reporters' )
8+ const chalk = require ( 'chalk' )
9+
10+ class Reporter extends DefaultReporter {
11+ constructor ( globalConfig , options ) {
12+ super ( globalConfig , options )
13+ }
14+
15+ printTestFileHeader ( testPath , config , result ) {
16+ // Don't call super to avoid printing console output
17+ const testFileName = testPath . replace ( process . cwd ( ) + '/' , '' )
18+ const duration = result . perfStats ? ( ( result . perfStats . end - result . perfStats . start ) / 1000 ) . toFixed ( 3 ) : '0.000'
19+ const status = result . numFailingTests === 0 ? chalk . green ( 'PASS' ) : chalk . red ( 'FAIL' )
20+
21+ this . log ( `${ status } ${ testFileName } ${ chalk . gray ( `(${ duration } s)` ) } ` )
22+
23+ // Print test structure
24+ const testsByGroup = { }
25+ result . testResults . forEach ( test => {
26+ const groupName = test . ancestorTitles [ 0 ] || 'Root'
27+ if ( ! testsByGroup [ groupName ] ) {
28+ testsByGroup [ groupName ] = [ ]
29+ }
30+ testsByGroup [ groupName ] . push ( test )
31+ } )
32+
33+ Object . entries ( testsByGroup ) . forEach ( ( [ groupName , tests ] ) => {
34+ if ( groupName !== 'Root' ) {
35+ this . log ( ` ${ groupName } ` )
36+ }
37+ tests . forEach ( test => {
38+ const prefix = groupName !== 'Root' ? ' ' : ' '
39+ const status = test . status === 'passed' ? chalk . green ( '✓' ) : chalk . red ( '✗' )
40+ const duration = test . duration ? chalk . gray ( ` (${ test . duration } ms)` ) : ''
41+ this . log ( `${ prefix } ${ status } ${ test . title } ${ duration } ` )
42+ } )
43+ } )
44+
45+ // Show console output only for failed tests
46+ if ( result . numFailingTests > 0 && result . console && result . console . length > 0 ) {
47+ result . console . forEach ( entry => {
48+ this . log ( ` ${ entry . type } ` )
49+ this . log ( ` ${ entry . message } ` )
50+ } )
51+ }
52+ }
53+
54+ onRunComplete ( contexts , results ) {
55+ this . log ( '' )
56+ const passedSuites = chalk . green ( `${ results . numPassedTestSuites } passed` )
57+ const failedSuites = results . numFailedTestSuites > 0 ? `, ${ chalk . red ( `${ results . numFailedTestSuites } failed` ) } ` : ''
58+ this . log ( `Test Suites: ${ passedSuites } ${ failedSuites } , ${ results . numTotalTestSuites } total` )
59+
60+ const passedTests = chalk . green ( `${ results . numPassedTests } passed` )
61+ const failedTests = results . numFailedTests > 0 ? `, ${ chalk . red ( `${ results . numFailedTests } failed` ) } ` : ''
62+ this . log ( `Tests: ${ passedTests } ${ failedTests } , ${ results . numTotalTests } total` )
63+
64+ this . log ( `Snapshots: ${ results . snapshot . total } total` )
65+ const duration = results . testRuntimeSummary ? ( results . testRuntimeSummary . slow / 1000 ) . toFixed ( 1 ) : '0.0'
66+ this . log ( `Time: ${ duration } s` )
67+ }
68+ }
69+
70+ module . exports = Reporter
0 commit comments