Skip to content

Commit 5704b34

Browse files
Dunqingsheremet-va
andauthored
feat(vitest): export all reporters in vitest/reporters (#3980)
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
1 parent 20263d9 commit 5704b34

File tree

7 files changed

+107
-1
lines changed

7 files changed

+107
-1
lines changed

docs/.vitepress/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ export default withPwa(defineConfig({
129129
text: 'Task Metadata',
130130
link: '/advanced/metadata',
131131
},
132+
{
133+
text: 'Extending default reporters',
134+
link: '/advanced/reporters',
135+
},
132136
],
133137
},
134138
],

docs/advanced/reporters.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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`

packages/vitest/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@
7777
"./coverage": {
7878
"types": "./coverage.d.ts",
7979
"import": "./dist/coverage.js"
80+
},
81+
"./reporters": {
82+
"types": "./dist/reporters.d.ts",
83+
"import": "./dist/reporters.js"
8084
}
8185
},
8286
"main": "./dist/index.js",

packages/vitest/reporters.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './dist/reporters.js'

packages/vitest/rollup.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const entries = [
3333
'src/coverage.ts',
3434
'src/public/utils.ts',
3535
'src/public/execute.ts',
36+
'src/public/reporters.ts',
3637
]
3738

3839
const dtsEntries = {
@@ -46,6 +47,7 @@ const dtsEntries = {
4647
coverage: 'src/coverage.ts',
4748
utils: 'src/public/utils.ts',
4849
execute: 'src/public/execute.ts',
50+
reporters: 'src/public/reporters.ts',
4951
}
5052

5153
const external = [

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Reporter } from '../../types'
12
import { BasicReporter } from './basic'
23
import { DefaultReporter } from './default'
34
import { DotReporter } from './dot'
@@ -7,8 +8,20 @@ import { TapReporter } from './tap'
78
import { JUnitReporter } from './junit'
89
import { TapFlatReporter } from './tap-flat'
910
import { HangingProcessReporter } from './hanging-process'
11+
import type { BaseReporter } from './base'
1012

11-
export { DefaultReporter }
13+
export {
14+
DefaultReporter,
15+
BasicReporter,
16+
DotReporter,
17+
JsonReporter,
18+
VerboseReporter,
19+
TapReporter,
20+
JUnitReporter,
21+
TapFlatReporter,
22+
HangingProcessReporter,
23+
}
24+
export type { BaseReporter, Reporter }
1225

1326
export const ReportersMap = {
1427
'default': DefaultReporter,
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../node/reporters'

0 commit comments

Comments
 (0)