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.
2233
2233
2234
2234
### onConsoleLog<NonProjectOption />
2235
2235
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
+ ```
2237
2243
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 .
2239
2245
2240
2246
Can be useful for filtering out logs from third - party libraries .
2241
2247
Original file line number Diff line number Diff line change @@ -433,9 +433,13 @@ export abstract class BaseReporter implements Reporter {
433
433
return false
434
434
}
435
435
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
+ }
439
443
}
440
444
return true
441
445
}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import type {
14
14
BuiltinReporterOptions ,
15
15
BuiltinReporters ,
16
16
} from '../reporters'
17
+ import type { TestCase , TestModule , TestSuite } from '../reporters/reported-tasks'
17
18
import type { TestSequencerConstructor } from '../sequencers/types'
18
19
import type { WatcherTriggerPattern } from '../watcher'
19
20
import type { BenchmarkUserOptions } from './benchmark'
@@ -661,7 +662,7 @@ export interface InlineConfig {
661
662
*
662
663
* Return `false` to ignore the log.
663
664
*/
664
- onConsoleLog ?: ( log : string , type : 'stdout' | 'stderr' ) => boolean | void
665
+ onConsoleLog ?: ( log : string , type : 'stdout' | 'stderr' , entity : TestModule | TestCase | TestSuite | undefined ) => boolean | void
665
666
666
667
/**
667
668
* 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'
2
2
import { expect , test } from 'vitest'
3
3
import { DefaultReporter } from 'vitest/reporters'
4
- import { runVitest } from '../../test-utils'
4
+ import { runInlineTests , runVitest } from '../../test-utils'
5
5
6
6
test ( 'can run custom pools with Vitest' , async ( ) => {
7
7
const reporter = new DefaultReporter ( )
@@ -72,3 +72,77 @@ test('can run custom pools with Vitest', async () => {
72
72
` )
73
73
}
74
74
} )
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