Skip to content

Commit ec2e804

Browse files
authored
fix(vitest): print file path instead of "unknown test" when logging (#4146)
1 parent 9350461 commit ec2e804

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

packages/vitest/src/node/reporters/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ErrorWithDiff, File, Reporter, Task, TaskResultPack, UserConsoleLo
44
import { getFullName, getSafeTimers, getSuites, getTests, hasFailed, hasFailedSnapshot, isCI, isNode, relativePath } from '../../utils'
55
import type { Vitest } from '../../node'
66
import { F_RIGHT } from '../../utils/figures'
7+
import { UNKNOWN_TEST_ID } from '../../runtime/console'
78
import { countTestErrors, divider, formatProjectName, formatTimeString, getStateString, getStateSymbol, pointer, renderSnapshotSummary } from './renderers/utils'
89

910
const BADGE_PADDING = ' '
@@ -186,7 +187,7 @@ export abstract class BaseReporter implements Reporter {
186187
if (!this.shouldLog(log))
187188
return
188189
const task = log.taskId ? this.ctx.state.idMap.get(log.taskId) : undefined
189-
const header = c.gray(log.type + c.dim(` | ${task ? getFullName(task, c.dim(' > ')) : 'unknown test'}`))
190+
const header = c.gray(log.type + c.dim(` | ${task ? getFullName(task, c.dim(' > ')) : log.taskId !== UNKNOWN_TEST_ID ? log.taskId : 'unknown test'}`))
190191
process[log.type].write(`${header}\n${log.content}\n`)
191192
}
192193

packages/vitest/src/runtime/console.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
import { Writable } from 'node:stream'
22
import { Console } from 'node:console'
3+
import { relative } from 'node:path'
34
import { getSafeTimers } from '@vitest/utils'
45
import { RealDate } from '../integrations/mock/date'
56
import type { WorkerGlobalState } from '../types'
67

8+
export const UNKNOWN_TEST_ID = '__vitest__unknown_test__'
9+
10+
function getTaskIdByStack(root: string) {
11+
const stack = new Error('STACK_TRACE_ERROR').stack?.split('\n')
12+
13+
if (!stack)
14+
return UNKNOWN_TEST_ID
15+
16+
const index = stack.findIndex(line => line.includes('at Console.value (node:internal/console/'))
17+
const line = index === -1 ? null : stack[index + 2]
18+
19+
if (!line)
20+
return UNKNOWN_TEST_ID
21+
22+
const filepath = line.match(/at\s(.*)\s?/)?.[1]
23+
24+
if (filepath)
25+
return relative(root, filepath)
26+
27+
return UNKNOWN_TEST_ID
28+
}
29+
730
export function createCustomConsole(state: WorkerGlobalState) {
831
const stdoutBuffer = new Map<string, any[]>()
932
const stderrBuffer = new Map<string, any[]>()
1033
const timers = new Map<string, { stdoutTime: number; stderrTime: number; timer: any }>()
11-
const unknownTestId = '__vitest__unknown_test__'
1234

1335
const { setTimeout, clearTimeout } = getSafeTimers()
1436

@@ -63,7 +85,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
6385

6486
const stdout = new Writable({
6587
write(data, encoding, callback) {
66-
const id = state?.current?.id ?? unknownTestId
88+
const id = state?.current?.id ?? getTaskIdByStack(state.ctx.config.root)
6789
let timer = timers.get(id)
6890
if (timer) {
6991
timer.stdoutTime = timer.stdoutTime || RealDate.now()
@@ -84,7 +106,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
84106
})
85107
const stderr = new Writable({
86108
write(data, encoding, callback) {
87-
const id = state?.current?.id ?? unknownTestId
109+
const id = state?.current?.id ?? getTaskIdByStack(state.ctx.config.root)
88110
let timer = timers.get(id)
89111
if (timer) {
90112
timer.stderrTime = timer.stderrTime || RealDate.now()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// eslint-disable-next-line no-console
2+
console.log('setup')
3+
4+
console.error('setup')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
import { runVitest } from '../../test-utils'
4+
5+
async function run() {
6+
return await runVitest({
7+
include: ['tests/fixtures/console.test.ts'],
8+
setupFiles: ['setupFiles/console-setup.ts'],
9+
})
10+
}
11+
12+
describe('setup files console', () => {
13+
test('print stdout and stderr correctly', async () => {
14+
const { stdout, stderr } = await run()
15+
const filepath = 'setupFiles/console-setup.ts'
16+
expect(stdout).toContain(`stdout | ${filepath}`)
17+
expect(stderr).toContain(`stderr | ${filepath}`)
18+
})
19+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'vitest'
2+
3+
test('empty', () => {})

0 commit comments

Comments
 (0)