Skip to content
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: 7 additions & 3 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ export abstract class BaseReporter implements Reporter {
return getTestName(test, separator)
}

protected getFullName(test: Task, separator?: string): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I don't like here is the use of Task since it's an experimental API, but to have the reported entity, we would have to rewrite the reporter which is probably outside of the scope of this PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is inline with protected getTestName(test: Task, separator?: string) above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but the previous one was there before the new API

return getFullName(test, separator)
}

protected formatShortError(error: ErrorWithDiff): string {
return `${F_RIGHT} ${error.message}`
}
Expand Down Expand Up @@ -374,7 +378,7 @@ export abstract class BaseReporter implements Reporter {
const task = log.taskId ? this.ctx.state.idMap.get(log.taskId) : undefined

if (task) {
headerText = getFullName(task, c.dim(' > '))
headerText = this.getFullName(task, c.dim(' > '))
}
else if (log.taskId && log.taskId !== '__vitest__unknown_test__') {
headerText = log.taskId
Expand Down Expand Up @@ -579,7 +583,7 @@ export abstract class BaseReporter implements Reporter {
continue
}

const groupName = getFullName(group, c.dim(' > '))
const groupName = this.getFullName(group, c.dim(' > '))
const project = this.ctx.projects.find(p => p.name === bench.file.projectName)

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

let name = getFullName(task, c.dim(' > '))
let name = this.getFullName(task, c.dim(' > '))

if (filepath) {
name += c.dim(` [ ${this.relative(filepath)} ]`)
Expand Down
12 changes: 12 additions & 0 deletions test/reporters/fixtures/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {expect, test } from 'vitest';

test('pass', ( { task }) => {
task.meta.custom = "Passing test added this"
});


test('fails', ( { task }) => {
task.meta.custom = "Failing test added this"

expect(true).toBe(false)
});
1 change: 1 addition & 0 deletions test/reporters/fixtures/metadata/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
18 changes: 17 additions & 1 deletion test/reporters/tests/default.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TestSpecification } from 'vitest/node'
import type { RunnerTask, TestSpecification } from 'vitest/node'
import { describe, expect, test } from 'vitest'
import { DefaultReporter } from 'vitest/reporters'
import { runVitest, runVitestCli } from '../../test-utils'

describe('default reporter', async () => {
Expand Down Expand Up @@ -238,6 +239,21 @@ describe('default reporter', async () => {
expect(stdout).toContain('Example project')
expect(stdout).toContain('\x1B[30m\x1B[45m Example project \x1B[49m\x1B[39m')
})

test('extended reporter can override getFullName', async () => {
class Custom extends DefaultReporter {
getFullName(test: RunnerTask, separator?: string): string {
return `${separator}{ name: ${test.name}, meta: ${test.meta.custom} } (Custom getFullName here)`
}
}

const { stderr } = await runVitest({
root: 'fixtures/metadata',
reporters: new Custom(),
})

expect(stderr).toMatch('FAIL > { name: fails, meta: Failing test added this } (Custom getFullName here')
Comment on lines +244 to +255
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lafeuil from #8018 👋

Is this the kind of use case you are trying to achieve?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is exactly that 👍

})
}, 120000)

function trimReporterOutput(report: string) {
Expand Down