Skip to content

Commit 27df68a

Browse files
authored
fix(reporter): task.meta should be available in custom reporter's errors (#8115)
1 parent 42eeb2e commit 27df68a

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ export abstract class BaseReporter implements Reporter {
249249
return getTestName(test, separator)
250250
}
251251

252+
protected getFullName(test: Task, separator?: string): string {
253+
return getFullName(test, separator)
254+
}
255+
252256
protected formatShortError(error: ErrorWithDiff): string {
253257
return `${F_RIGHT} ${error.message}`
254258
}
@@ -374,7 +378,7 @@ export abstract class BaseReporter implements Reporter {
374378
const task = log.taskId ? this.ctx.state.idMap.get(log.taskId) : undefined
375379

376380
if (task) {
377-
headerText = getFullName(task, c.dim(' > '))
381+
headerText = this.getFullName(task, c.dim(' > '))
378382
}
379383
else if (log.taskId && log.taskId !== '__vitest__unknown_test__') {
380384
headerText = log.taskId
@@ -579,7 +583,7 @@ export abstract class BaseReporter implements Reporter {
579583
continue
580584
}
581585

582-
const groupName = getFullName(group, c.dim(' > '))
586+
const groupName = this.getFullName(group, c.dim(' > '))
583587
const project = this.ctx.projects.find(p => p.name === bench.file.projectName)
584588

585589
this.log(` ${formatProjectName(project)}${bench.name}${c.dim(` - ${groupName}`)}`)
@@ -636,7 +640,7 @@ export abstract class BaseReporter implements Reporter {
636640
const projectName = (task as File)?.projectName || task.file?.projectName || ''
637641
const project = this.ctx.projects.find(p => p.name === projectName)
638642

639-
let name = getFullName(task, c.dim(' > '))
643+
let name = this.getFullName(task, c.dim(' > '))
640644

641645
if (filepath) {
642646
name += c.dim(` [ ${this.relative(filepath)} ]`)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {expect, test } from 'vitest';
2+
3+
test('pass', ( { task }) => {
4+
task.meta.custom = "Passing test added this"
5+
});
6+
7+
8+
test('fails', ( { task }) => {
9+
task.meta.custom = "Failing test added this"
10+
11+
expect(true).toBe(false)
12+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {}

test/reporters/tests/default.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { TestSpecification } from 'vitest/node'
1+
import type { RunnerTask, TestSpecification } from 'vitest/node'
22
import { describe, expect, test } from 'vitest'
3+
import { DefaultReporter } from 'vitest/reporters'
34
import { runVitest, runVitestCli } from '../../test-utils'
45

56
describe('default reporter', async () => {
@@ -238,6 +239,21 @@ describe('default reporter', async () => {
238239
expect(stdout).toContain('Example project')
239240
expect(stdout).toContain('\x1B[30m\x1B[45m Example project \x1B[49m\x1B[39m')
240241
})
242+
243+
test('extended reporter can override getFullName', async () => {
244+
class Custom extends DefaultReporter {
245+
getFullName(test: RunnerTask, separator?: string): string {
246+
return `${separator}{ name: ${test.name}, meta: ${test.meta.custom} } (Custom getFullName here)`
247+
}
248+
}
249+
250+
const { stderr } = await runVitest({
251+
root: 'fixtures/metadata',
252+
reporters: new Custom(),
253+
})
254+
255+
expect(stderr).toMatch('FAIL > { name: fails, meta: Failing test added this } (Custom getFullName here')
256+
})
241257
}, 120000)
242258

243259
function trimReporterOutput(report: string) {

0 commit comments

Comments
 (0)