Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
/ base-reporter Public archive

Base reporter to create customized tests reporter for Japa

License

Notifications You must be signed in to change notification settings

japa/base-reporter

Repository files navigation

Japa Base Reporter

Base reporter to create customized testing reporters for Japa

The Base reporter abstracts the repetitive parts of creating a tests reporters.

github-actions-image npm-image license-image typescript-image

Setup

Install the package from npm registry as follows:

npm i @japa/base-reporter

# Yarn lovers
yarn add @japa/base-reporter
import { BaseReporter } from '@japa/base-reporter'

class MyReporter extends BaseReporter {}

export const reporterFn = (myReporterOptions = {}) => {
  const myReporter = new MyReporter(myReporterOptions)
  return myReporter.boot.bind(reporter)
}

Handlers

The Base reporter invokes following methods as it receives the events from the runner. You can implement these methods to display the tests progress.

import type {
  TestEndNode,
  SuiteEndNode,
  GroupEndNode,
  TestStartNode,
  RunnerEndNode,
  GroupStartNode,
  SuiteStartNode,
  RunnerStartNode,
} from '@japa/core'

class SpecReporter extends BaseReporter {
  protected onTestStart(payload: TestStartNode) {
    console.log('test started')
  }
  protected onTestEnd(payload: TestEndNode) {
    console.log('test endeded')
  }

  protected onGroupStart(payload: GroupStartNode) {
    console.log('group started')
  }
  protected onGroupEnd(payload: GroupEndNode) {
    console.log('group ended')
  }

  protected onSuiteStart(payload: SuiteStartNode) {
    console.log('suite started')
  }
  protected onSuiteEnd(payload: SuiteEndNode) {
    console.log('suite ended')
  }

  protected async start(payload: RunnerStartNode) {
    console.log('test runner started. You can run async operations here')
  }
  protected async end(payload: RunnerEndNode) {
    console.log('test runner ended. You can run async operations here')
  }
}

Inherited properties

The following properties are available on the BaseReporter. These properties are available only after the boot method is called.

runner

Reference to underlying tests runner instance.

this.runner

currentFileName

Reference to the file name for which tests are getting executed. The filename is only available inside the test or group handlers.

this.currentFileName

currentSuiteName

Reference to the suite name for which tests are getting executed. The suite name is only available after the onSuiteStart handler call.

this.currentSuiteName

uncaughtExceptions

Uncaught exceptions collected while tests are running. We rely on process.on('uncaughtException') event to collect uncaught exceptions and display them with their stack trace at the end.

Printing tests summary

After all the tests have been finished, you can call the printSummary method to print a detailed summary of all tests alongside pretty diffs and pretty error stack trace.

You should call the printSummary method from the end handler.

class SpecReporter extends BaseReporter {
  protected async end() {
    const summary = await this.runner.getSummary()
    await this.printSummary(summary)
  }
}