File tree Expand file tree Collapse file tree 4 files changed +93
-8
lines changed Expand file tree Collapse file tree 4 files changed +93
-8
lines changed Original file line number Diff line number Diff line change @@ -2233,9 +2233,15 @@ Retry the test specific number of times if it fails.
22332233
22342234### onConsoleLog<NonProjectOption />
22352235
2236- - ** Type** : ` (log: string, type: 'stdout' | 'stderr') => boolean | void `
2236+ ``` ts
2237+ function onConsoleLog(
2238+ log : string ,
2239+ type : ' stdout' | ' stderr' ,
2240+ entity : TestModule | TestSuite | TestCase | undefined ,
2241+ ): boolean | void
2242+ ```
22372243
2238- Custom handler for ` console.log ` in tests. If you return ` false ` , Vitest will not print the log to the console.
2244+ Custom handler for ` console ` methods in tests . If you return ` false ` , Vitest will not print the log to the console . Note that Vitest ignores all other falsy values .
22392245
22402246Can be useful for filtering out logs from third - party libraries .
22412247
Original file line number Diff line number Diff line change @@ -433,9 +433,13 @@ export abstract class BaseReporter implements Reporter {
433433 return false
434434 }
435435
436- const shouldLog = this . ctx . config . onConsoleLog ?.( log . content , log . type )
437- if ( shouldLog === false ) {
438- return shouldLog
436+ if ( this . ctx . config . onConsoleLog ) {
437+ const task = log . taskId ? this . ctx . state . idMap . get ( log . taskId ) : undefined
438+ const entity = task && this . ctx . state . getReportedEntity ( task )
439+ const shouldLog = this . ctx . config . onConsoleLog ( log . content , log . type , entity )
440+ if ( shouldLog === false ) {
441+ return shouldLog
442+ }
439443 }
440444 return true
441445 }
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import type {
1414 BuiltinReporterOptions ,
1515 BuiltinReporters ,
1616} from '../reporters'
17+ import type { TestCase , TestModule , TestSuite } from '../reporters/reported-tasks'
1718import type { TestSequencerConstructor } from '../sequencers/types'
1819import type { WatcherTriggerPattern } from '../watcher'
1920import type { BenchmarkUserOptions } from './benchmark'
@@ -661,7 +662,7 @@ export interface InlineConfig {
661662 *
662663 * Return `false` to ignore the log.
663664 */
664- onConsoleLog ?: ( log : string , type : 'stdout' | 'stderr' ) => boolean | void
665+ onConsoleLog ?: ( log : string , type : 'stdout' | 'stderr' , entity : TestModule | TestCase | TestSuite | undefined ) => boolean | void
665666
666667 /**
667668 * Enable stack trace filtering. If absent, all stack trace frames
Original file line number Diff line number Diff line change 1- import { resolve } from 'pathe'
1+ import { relative , resolve } from 'pathe'
22import { expect , test } from 'vitest'
33import { DefaultReporter } from 'vitest/reporters'
4- import { runVitest } from '../../test-utils'
4+ import { runInlineTests , runVitest } from '../../test-utils'
55
66test ( 'can run custom pools with Vitest' , async ( ) => {
77 const reporter = new DefaultReporter ( )
@@ -72,3 +72,77 @@ test('can run custom pools with Vitest', async () => {
7272 ` )
7373 }
7474} )
75+
76+ test ( 'onConsoleLog receives the entity' , async ( ) => {
77+ const logs : {
78+ log : string
79+ type : 'stderr' | 'stdout'
80+ entity : { type : string ; name : string } | undefined
81+ } [ ] = [ ]
82+ const { stderr } = await runInlineTests (
83+ {
84+ 'basic.test.ts' : `
85+ console.log('module')
86+
87+ describe('suite', () => {
88+ beforeAll(() => {
89+ console.log('suite')
90+ })
91+
92+ test('test', () => {
93+ console.log('test')
94+ })
95+ })
96+ ` ,
97+ } ,
98+ {
99+ globals : true ,
100+ onConsoleLog ( log , type , entity ) {
101+ logs . push ( {
102+ log,
103+ type,
104+ entity : entity
105+ ? {
106+ type : entity . type ,
107+ name : entity . type === 'module'
108+ ? relative ( entity . project . config . root , entity . moduleId )
109+ : entity . name ,
110+ }
111+ : undefined ,
112+ } )
113+ } ,
114+ } ,
115+ )
116+ expect ( stderr ) . toBe ( '' )
117+ expect ( logs ) . toMatchInlineSnapshot ( `
118+ [
119+ {
120+ "entity": {
121+ "name": "basic.test.ts",
122+ "type": "module",
123+ },
124+ "log": "module
125+ ",
126+ "type": "stdout",
127+ },
128+ {
129+ "entity": {
130+ "name": "suite",
131+ "type": "suite",
132+ },
133+ "log": "suite
134+ ",
135+ "type": "stdout",
136+ },
137+ {
138+ "entity": {
139+ "name": "test",
140+ "type": "test",
141+ },
142+ "log": "test
143+ ",
144+ "type": "stdout",
145+ },
146+ ]
147+ ` )
148+ } )
You can’t perform that action at this time.
0 commit comments