Halestorm Dev

Jest Custom Reporter

By David Hale on Feb 25, 2019

In this blog post we will dive into handling custom actions before, during, or after your test runs in Jest.

For example, my team needed to find a way to get Sauce Labs updated with the appropriate test data to enhance traceability on all our end to end tests but we wanted an ability to define out a system to extend past this first offering.

Jest allows you to hook into lifecycle methods like onTestStart, onTestResult, and onRunComplete (as well as others)

We leverage TypeScript for our automation framework and reworked the example custom reporter to work with the Jest types like so:

class SauceLabReporter {
  constructor(public globalConfig: jest.GlobalConfig, private options: any) {}

  public onTestStart(test: jest.Test) {
   // runs on start
  }

  public onTestResult(test: jest.Test, testResult: jest.TestResult, results: jest.AggregatedResult) {
    // runs on result (skipped/passed/failed/pending)
  }

  public onRunComplete(contexts: Set<jest.Context>, results: jest.AggregatedResult) {
    // runs after every test is complete
  }
}

module.exports = SauceLabReporter;

After creating the skeleton it was just understanding the structure of the objects (jest.AggregatedResult, jest.Test, and jest.TestResult) and then hooking it in properly to our jest configuration. (custom reporters can be leveraged locally and published as npm packages themselves)

Like has been before, the implications moving forward for this custom reporter can be easily expanded upon to not only handle test reporter functionality but do further custom downstream actions.