Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vitest): export all reporters in vitest/reporters #3980

Merged
merged 6 commits into from Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/.vitepress/config.ts
Expand Up @@ -129,6 +129,10 @@ export default withPwa(defineConfig({
text: 'Task Metadata',
link: '/advanced/metadata',
},
{
text: 'Extending default reporters',
link: '/advanced/reporters',
},
],
},
],
Expand Down
81 changes: 81 additions & 0 deletions docs/advanced/reporters.md
@@ -0,0 +1,81 @@
# Extending default reporters

You can import reporters from `vitest/reporters` 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.

```ts
import { DefaultReporter } from 'vitest/reporters'

export default class MyDefaultReporter extends DefaultReporter {
// do something
}
```

Of course, you can create your reporter from scratch. Just extend the `BaseReporter` class and implement the methods you need.

And here is an example of a custom reporter:

```ts
// ./custom-reporter.ts
import { BaseReporter } from 'vitest/reporters'

export default class CustomReporter extends BaseReporter {
onCollected() {
const files = this.ctx.state.getFiles(this.watchFilters)
this.reportTestSummary(files)
}
}
```

Or implement the `Reporter` interface:

```ts
// ./custom-reporter.ts
import { Reporter } from 'vitest/reporters'

export default class CustomReporter implements Reporter {
onCollected() {
// print something
}
}
```

Then you can use your custom reporter in the `vitest.config.ts` file:

```ts
import { defineConfig } from 'vitest'
Dunqing marked this conversation as resolved.
Show resolved Hide resolved
import CustomReporter from './custom-reporter'
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

export default defineConfig({
test: {
reporter: CustomReporter,
Dunqing marked this conversation as resolved.
Show resolved Hide resolved
},
})
```

## Exported reporters

Vitest comes with a few built-in reporters that you can use out of the box.

### Built-in reporters:

1. `BasicReporter`
1. `DefaultReporter`
2. `DotReporter`
3. `JsonReporter`
4. `VerboseReporter`
5. `TapReporter`
6. `JUnitReporter`
7. `TapFlatReporter`
8. `HangingProcessReporter`

### Base Abstract reporters:

1. `BaseReporter`

### Interface reporters:

1. `Reporter`