Skip to content
Vitest 3
Main Navigation Guide & APIConfigBrowser ModeAdvanced API
3.2.0
2.1.9
1.6.1
0.34.6

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

Appearance

Sidebar Navigation

API

Node API

Getting Started

Vitest

TestProject

TestSpecification

Test Task API

TestCase

TestSuite

TestModule

TestCollection

Plugin API

Runner API

Reporters API

Task Metadata

Guides

Running Tests

Extending Reporters

Custom Pool

Config Reference

Test API Reference

On this page

Extending Reporters ​

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/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
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
import type { Reporter } from 'vitest/node';

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()],
  },
});

Reported Tasks ​

Instead of using the tasks that reporters receive, it is recommended to use the Reported Tasks API instead.

You can get access to this API by calling vitest.state.getReportedEntity(runnerTask):

ts
import type { Reporter, RunnerTestFile, TestModule, Vitest } from 'vitest/node';

class MyReporter implements Reporter {
  private vitest!: Vitest;

  onInit(vitest: Vitest) {
    this.vitest = vitest;
  }

  onFinished(files: RunnerTestFile[]) {
    for (const file of files) {
      // note that the old task implementation uses "file" instead of "module"
      const testModule = this.vitest.state.getReportedEntity(
        file
      ) as TestModule;
      for (const task of testModule.children) {
        //                          ^?
        console.log('finished', 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: ​

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

Base Abstract reporters: ​

  1. BaseReporter

Interface reporters: ​

  1. Reporter
Pager
Previous pageRunning Tests
Next pageCustom Pool

Released under the MIT License.

Copyright (c) 2021-Present Vitest Team

https://vitest.dev/advanced/reporters

Released under the MIT License.

Copyright (c) 2021-Present Vitest Team