@@ -2,6 +2,7 @@ import type { File, Task, TaskResultPack } from '@vitest/runner'
22import type { ErrorWithDiff , UserConsoleLog } from '../../types/general'
33import type { Vitest } from '../core'
44import type { Reporter } from '../types/reporter'
5+ import type { TestCase , TestModule , TestResult , TestSuite } from './reported-tasks'
56import { performance } from 'node:perf_hooks'
67import { getFullName , getSuites , getTestName , getTests , hasFailed } from '@vitest/runner/utils'
78import { toArray } from '@vitest/utils'
@@ -66,6 +67,32 @@ export abstract class BaseReporter implements Reporter {
6667 }
6768 }
6869
70+ onTestCaseResult ( testCase : TestCase ) : void {
71+ if ( testCase . result ( ) . state === 'failed' ) {
72+ this . logFailedTask ( testCase . task )
73+ }
74+ }
75+
76+ onTestSuiteResult ( testSuite : TestSuite ) : void {
77+ if ( testSuite . state ( ) === 'failed' ) {
78+ this . logFailedTask ( testSuite . task )
79+ }
80+ }
81+
82+ onTestModuleEnd ( testModule : TestModule ) : void {
83+ if ( testModule . state ( ) === 'failed' ) {
84+ this . logFailedTask ( testModule . task )
85+ }
86+ }
87+
88+ private logFailedTask ( task : Task ) {
89+ if ( this . ctx . config . silent === 'passed-only' ) {
90+ for ( const log of task . logs || [ ] ) {
91+ this . onUserConsoleLog ( log , 'failed' )
92+ }
93+ }
94+ }
95+
6996 onTaskUpdate ( packs : TaskResultPack [ ] ) : void {
7097 for ( const pack of packs ) {
7198 const task = this . ctx . state . idMap . get ( pack [ 0 ] )
@@ -272,8 +299,8 @@ export abstract class BaseReporter implements Reporter {
272299 this . start = performance . now ( )
273300 }
274301
275- onUserConsoleLog ( log : UserConsoleLog ) : void {
276- if ( ! this . shouldLog ( log ) ) {
302+ onUserConsoleLog ( log : UserConsoleLog , taskState ?: TestResult [ 'state' ] ) : void {
303+ if ( ! this . shouldLog ( log , taskState ) ) {
277304 return
278305 }
279306
@@ -334,10 +361,15 @@ export abstract class BaseReporter implements Reporter {
334361 this . log ( c . yellow ( 'Test removed...' ) + ( trigger ? c . dim ( ` [ ${ this . relative ( trigger ) } ]\n` ) : '' ) )
335362 }
336363
337- shouldLog ( log : UserConsoleLog ) : boolean {
338- if ( this . ctx . config . silent ) {
364+ shouldLog ( log : UserConsoleLog , taskState ?: TestResult [ 'state' ] ) : boolean {
365+ if ( this . ctx . config . silent === true ) {
339366 return false
340367 }
368+
369+ if ( this . ctx . config . silent === 'passed-only' && taskState !== 'failed' ) {
370+ return false
371+ }
372+
341373 const shouldLog = this . ctx . config . onConsoleLog ?.( log . content , log . type )
342374 if ( shouldLog === false ) {
343375 return shouldLog
0 commit comments