|
| 1 | +# Extending default reporters |
| 2 | + |
| 3 | +You can import reporters from `vitest/reporters` and extend them to create your custom reporters. |
| 4 | + |
| 5 | +## Extending built-in reporters |
| 6 | + |
| 7 | +In general, you don't need to create your reporter from scratch. `vitest` comes with several default reporting programs that you can extend. |
| 8 | + |
| 9 | +```ts |
| 10 | +import { DefaultReporter } from 'vitest/reporters' |
| 11 | + |
| 12 | +export default class MyDefaultReporter extends DefaultReporter { |
| 13 | + // do something |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +Of course, you can create your reporter from scratch. Just extend the `BaseReporter` class and implement the methods you need. |
| 18 | + |
| 19 | +And here is an example of a custom reporter: |
| 20 | + |
| 21 | +```ts |
| 22 | +// ./custom-reporter.ts |
| 23 | +import { BaseReporter } from 'vitest/reporters' |
| 24 | + |
| 25 | +export default class CustomReporter extends BaseReporter { |
| 26 | + onCollected() { |
| 27 | + const files = this.ctx.state.getFiles(this.watchFilters) |
| 28 | + this.reportTestSummary(files) |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +Or implement the `Reporter` interface: |
| 34 | + |
| 35 | +```ts |
| 36 | +// ./custom-reporter.ts |
| 37 | +import { Reporter } from 'vitest/reporters' |
| 38 | + |
| 39 | +export default class CustomReporter implements Reporter { |
| 40 | + onCollected() { |
| 41 | + // print something |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Then you can use your custom reporter in the `vitest.config.ts` file: |
| 47 | + |
| 48 | +```ts |
| 49 | +import { defineConfig } from 'vitest/config' |
| 50 | +import CustomReporter from './custom-reporter.js' |
| 51 | + |
| 52 | +export default defineConfig({ |
| 53 | + test: { |
| 54 | + reporters: [new CustomReporter()], |
| 55 | + }, |
| 56 | +}) |
| 57 | +``` |
| 58 | + |
| 59 | +## Exported reporters |
| 60 | + |
| 61 | +`vitest` comes with a few built-in reporters that you can use out of the box. |
| 62 | + |
| 63 | +### Built-in reporters: |
| 64 | + |
| 65 | +1. `BasicReporter` |
| 66 | +1. `DefaultReporter` |
| 67 | +2. `DotReporter` |
| 68 | +3. `JsonReporter` |
| 69 | +4. `VerboseReporter` |
| 70 | +5. `TapReporter` |
| 71 | +6. `JUnitReporter` |
| 72 | +7. `TapFlatReporter` |
| 73 | +8. `HangingProcessReporter` |
| 74 | + |
| 75 | +### Base Abstract reporters: |
| 76 | + |
| 77 | +1. `BaseReporter` |
| 78 | + |
| 79 | +### Interface reporters: |
| 80 | + |
| 81 | +1. `Reporter` |
0 commit comments