From 5704b341f1ef411d9cd71fb343d19c64d9d19943 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 22 Aug 2023 00:31:31 +0800 Subject: [PATCH] feat(vitest): export all reporters in `vitest/reporters` (#3980) Co-authored-by: Vladimir --- docs/.vitepress/config.ts | 4 + docs/advanced/reporters.md | 81 +++++++++++++++++++++ packages/vitest/package.json | 4 + packages/vitest/reporters.d.ts | 1 + packages/vitest/rollup.config.js | 2 + packages/vitest/src/node/reporters/index.ts | 15 +++- packages/vitest/src/public/reporters.ts | 1 + 7 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 docs/advanced/reporters.md create mode 100644 packages/vitest/reporters.d.ts create mode 100644 packages/vitest/src/public/reporters.ts diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 28e6fc6b03f8..abb549416187 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -129,6 +129,10 @@ export default withPwa(defineConfig({ text: 'Task Metadata', link: '/advanced/metadata', }, + { + text: 'Extending default reporters', + link: '/advanced/reporters', + }, ], }, ], diff --git a/docs/advanced/reporters.md b/docs/advanced/reporters.md new file mode 100644 index 000000000000..26ce47a5432f --- /dev/null +++ b/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/config' +import CustomReporter from './custom-reporter.js' + +export default defineConfig({ + test: { + reporters: [new CustomReporter()], + }, +}) +``` + +## 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` diff --git a/packages/vitest/package.json b/packages/vitest/package.json index f015b0aabafa..3b3411b5718a 100644 --- a/packages/vitest/package.json +++ b/packages/vitest/package.json @@ -77,6 +77,10 @@ "./coverage": { "types": "./coverage.d.ts", "import": "./dist/coverage.js" + }, + "./reporters": { + "types": "./dist/reporters.d.ts", + "import": "./dist/reporters.js" } }, "main": "./dist/index.js", diff --git a/packages/vitest/reporters.d.ts b/packages/vitest/reporters.d.ts new file mode 100644 index 000000000000..5fe5fe7e27af --- /dev/null +++ b/packages/vitest/reporters.d.ts @@ -0,0 +1 @@ +export * from './dist/reporters.js' diff --git a/packages/vitest/rollup.config.js b/packages/vitest/rollup.config.js index 7832b7e220ec..8c154a621d60 100644 --- a/packages/vitest/rollup.config.js +++ b/packages/vitest/rollup.config.js @@ -33,6 +33,7 @@ const entries = [ 'src/coverage.ts', 'src/public/utils.ts', 'src/public/execute.ts', + 'src/public/reporters.ts', ] const dtsEntries = { @@ -46,6 +47,7 @@ const dtsEntries = { coverage: 'src/coverage.ts', utils: 'src/public/utils.ts', execute: 'src/public/execute.ts', + reporters: 'src/public/reporters.ts', } const external = [ diff --git a/packages/vitest/src/node/reporters/index.ts b/packages/vitest/src/node/reporters/index.ts index 60e8f2babc9a..0bece51d7db2 100644 --- a/packages/vitest/src/node/reporters/index.ts +++ b/packages/vitest/src/node/reporters/index.ts @@ -1,3 +1,4 @@ +import type { Reporter } from '../../types' import { BasicReporter } from './basic' import { DefaultReporter } from './default' import { DotReporter } from './dot' @@ -7,8 +8,20 @@ import { TapReporter } from './tap' import { JUnitReporter } from './junit' import { TapFlatReporter } from './tap-flat' import { HangingProcessReporter } from './hanging-process' +import type { BaseReporter } from './base' -export { DefaultReporter } +export { + DefaultReporter, + BasicReporter, + DotReporter, + JsonReporter, + VerboseReporter, + TapReporter, + JUnitReporter, + TapFlatReporter, + HangingProcessReporter, +} +export type { BaseReporter, Reporter } export const ReportersMap = { 'default': DefaultReporter, diff --git a/packages/vitest/src/public/reporters.ts b/packages/vitest/src/public/reporters.ts new file mode 100644 index 000000000000..eca01ade4d0b --- /dev/null +++ b/packages/vitest/src/public/reporters.ts @@ -0,0 +1 @@ +export * from '../node/reporters'