Skip to content

feat: provide entity to onConsoleLog #8159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2323,9 +2323,15 @@ Retry the test specific number of times if it fails.

### onConsoleLog<NonProjectOption />

- **Type**: `(log: string, type: 'stdout' | 'stderr') => boolean | void`
```ts
function onConsoleLog(
log: string,
type: 'stdout' | 'stderr',
entity: TestModule | TestSuite | TestCase | undefined,
): boolean | void
```

Custom handler for `console.log` in tests. If you return `false`, Vitest will not print the log to the console.
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.

Can be useful for filtering out logs from third-party libraries.

Expand Down
10 changes: 7 additions & 3 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,13 @@ export abstract class BaseReporter implements Reporter {
return false
}

const shouldLog = this.ctx.config.onConsoleLog?.(log.content, log.type)
if (shouldLog === false) {
return shouldLog
if (this.ctx.config.onConsoleLog) {
const task = log.taskId ? this.ctx.state.idMap.get(log.taskId) : undefined
const entity = task && this.ctx.state.getReportedEntity(task)
const shouldLog = this.ctx.config.onConsoleLog(log.content, log.type, entity)
if (shouldLog === false) {
return shouldLog
}
}
return true
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/node/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
BuiltinReporterOptions,
BuiltinReporters,
} from '../reporters'
import type { TestCase, TestModule, TestSuite } from '../reporters/reported-tasks'
import type { TestSequencerConstructor } from '../sequencers/types'
import type { WatcherTriggerPattern } from '../watcher'
import type { BenchmarkUserOptions } from './benchmark'
Expand Down Expand Up @@ -653,7 +654,7 @@ export interface InlineConfig {
*
* Return `false` to ignore the log.
*/
onConsoleLog?: (log: string, type: 'stdout' | 'stderr') => boolean | void
onConsoleLog?: (log: string, type: 'stdout' | 'stderr', entity: TestModule | TestCase | TestSuite | undefined) => boolean | void

/**
* Enable stack trace filtering. If absent, all stack trace frames
Expand Down
78 changes: 76 additions & 2 deletions test/cli/test/console.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from 'pathe'
import { relative, resolve } from 'pathe'
import { expect, test } from 'vitest'
import { DefaultReporter } from 'vitest/reporters'
import { runVitest } from '../../test-utils'
import { runInlineTests, runVitest } from '../../test-utils'

test('can run custom pools with Vitest', async () => {
const reporter = new DefaultReporter()
Expand Down Expand Up @@ -72,3 +72,77 @@ test('can run custom pools with Vitest', async () => {
`)
}
})

test('onConsoleLog receives the entity', async () => {
const logs: {
log: string
type: 'stderr' | 'stdout'
entity: { type: string; name: string } | undefined
}[] = []
const { stderr } = await runInlineTests(
{
'basic.test.ts': `
console.log('module')

describe('suite', () => {
beforeAll(() => {
console.log('suite')
})

test('test', () => {
console.log('test')
})
})
`,
},
{
globals: true,
onConsoleLog(log, type, entity) {
logs.push({
log,
type,
entity: entity
? {
type: entity.type,
name: entity.type === 'module'
? relative(entity.project.config.root, entity.moduleId)
: entity.name,
}
: undefined,
})
},
},
)
expect(stderr).toBe('')
expect(logs).toMatchInlineSnapshot(`
[
{
"entity": {
"name": "basic.test.ts",
"type": "module",
},
"log": "module
",
"type": "stdout",
},
{
"entity": {
"name": "suite",
"type": "suite",
},
"log": "suite
",
"type": "stdout",
},
{
"entity": {
"name": "test",
"type": "test",
},
"log": "test
",
"type": "stdout",
},
]
`)
})