MCPcopy
hub / github.com/vitest-dev/vitest / onUserConsoleLog

Function onUserConsoleLog

packages/vitest/src/node/reporters/base.ts:448–504  ·  view source on GitHub ↗
(log: UserConsoleLog, taskState?: TestResult['state'])

Source from the content-addressed store, hash-verified

446 }
447
448 onUserConsoleLog(log: UserConsoleLog, taskState?: TestResult['state']): void {
449 if (!this.shouldLog(log, taskState)) {
450 return
451 }
452
453 const output
454 = log.type === 'stdout'
455 ? this.ctx.logger.outputStream
456 : this.ctx.logger.errorStream
457
458 const write = (msg: string) => (output as any).write(msg)
459
460 let headerText = 'unknown test'
461 const task = log.taskId ? this.ctx.state.idMap.get(log.taskId) : undefined
462
463 if (task) {
464 headerText = this.getFullName(task, separator)
465 }
466 else if (log.taskId && log.taskId !== '__vitest__unknown_test__') {
467 headerText = log.taskId
468 }
469
470 write(c.gray(log.type + c.dim(` | ${headerText}\n`)) + log.content)
471
472 if (log.origin) {
473 // browser logs don't have an extra end of line at the end like Node.js does
474 if (log.browser) {
475 write('\n')
476 }
477
478 const project = task
479 ? this.ctx.getProjectByName(task.file.projectName || '')
480 : this.ctx.getRootProject()
481
482 const stack = log.browser
483 ? (project.browser?.parseStacktrace(log.origin) || [])
484 : parseStacktrace(log.origin)
485
486 const highlight = task && stack.find(i => i.file === task.file.filepath)
487
488 for (const frame of stack) {
489 const color = frame === highlight ? c.cyan : c.gray
490 const path = relative(project.config.root, frame.file)
491
492 const positions = [
493 frame.method,
494 `${path}:${c.dim(`${frame.line}:${frame.column}`)}`,
495 ]
496 .filter(Boolean)
497 .join(' ')
498
499 write(color(` ${c.dim(F_POINTER)} ${positions}\n`))
500 }
501 }
502
503 write('\n')
504 }
505

Callers

nothing calls this directly

Calls 9

parseStacktraceFunction · 0.90
getFullNameMethod · 0.80
getProjectByNameMethod · 0.80
getRootProjectMethod · 0.80
writeFunction · 0.70
relativeFunction · 0.70
filterMethod · 0.65
getMethod · 0.45
parseStacktraceMethod · 0.45

Tested by

no test coverage detected