Extending Reporters advanced
WARNING
This is an advanced API. If you just want to configure built-in reporters, read the "Reporters" guide.
You can import reporters from vitest/node and extend them to create your custom reporters.
Extending Built-in Reporters
In general, you don't need to create your reporter from scratch. vitest comes with several default reporting programs that you can extend.
import { DefaultReporter } from 'vitest/node'
export default class MyDefaultReporter extends DefaultReporter {
// do something
}WARNING
However, note that exposed reports are not considered stable and can change the shape of their API within a minor version.
Of course, you can create your reporter from scratch. Just implement the Reporter interface:
And here is an example of a custom reporter:
import type { Reporter } from 'vitest/node'
export default class CustomReporter implements Reporter {
onTestModuleCollected(testModule) {
console.log(testModule.moduleId, 'is finished')
for (const test of testModule.children.allTests()) {
console.log(test.name, test.result().state)
}
}
}Then you can use your custom reporter in the vitest.config.ts file:
import { defineConfig } from 'vitest/config'
import CustomReporter from './custom-reporter.js'
export default defineConfig({
test: {
reporters: [new CustomReporter()],
},
})Reported Tasks
Reported events receive tasks for tests, suites and modules:
import type { Reporter, TestModule } from 'vitest/node'
class MyReporter implements Reporter {
onTestRunEnd(testModules: ReadonlyArray<TestModule>) {
for (const testModule of testModules) {
for (const task of testModule.children) {
console.log('test run end', task.type, task.fullName)
}
}
}
}Exported Reporters
vitest comes with a few built-in reporters that you can use out of the box.
Built-in reporters:
DefaultReporterDotReporterJsonReporterVerboseReporterTapReporterJUnitReporterTapFlatReporterHangingProcessReporterTreeReporter
Interface reporters:
Reporter