Skip to content

Coverage reporter configuration improvements #2806

Open
@miyasudokoro

Description

@miyasudokoro

I need to be able to configure the watermarks and output subdirectories/files for coverage reporting. Below is the kind of change I would make for a pull request for this issue for writeCoverageReport.ts.

import reports from 'istanbul-reports';
import libReport from 'istanbul-lib-report';

import { CoverageConfig } from '../config/TestRunnerCoreConfig';
import { TestCoverage } from '../coverage/getTestCoverage';

export function writeCoverageReport(testCoverage: TestCoverage, config: CoverageConfig) {
  // make useful watermarks
  let watermarks = {};
  if ( config.watermarks ) {
    watermarks = config.watermarks;
  } else if ( config.threshold ) {
    for ( const setting in config.threshold ) {
      watermarks[ setting ] = [ Math.max( config.threshold[ setting ] - 5, 0 ), config.threshold[ setting ] ]
    }
  }

  // create a context for report generation
  const context = libReport.createContext({
    dir: config.reportDir,
    watermarks,
    coverageMap: testCoverage.coverageMap,
  });

  const reporters = config.reporters || [];

  for (const reporter of reporters) {
    const conf = config.reporterConfigurations?.[ reporter ] || {};
    const report = reports.create(reporter, {
      projectRoot: process.cwd(),
      maxCols: process.stdout.columns || 100,
      ...conf
    });
    (report as any).execute(context);
  }
}

In my proposed change, the watermarks are set based on the threshold settings such that coverage that almost reaches the threshold is yellow, while coverage well below the threshold is red. The current code defaults (and also the defaults found inside Istanbul itself) of [50, 80] are useless for my projects, which have thresholds of 90%. If people don't want this feature, I could leave it out, but I would still definitely want to allow CoverageConfig to provide a "watermarks" override.

Here's an example configuration.

{
    "reportDir": "reports",
    "reporters": [ "text", "html", "cobertura" ],
    "threshold": {
        "statements": 90,
        "lines": 90,
        "branches": 90,
        "functions": 90
    },
    "reporterConfigurations": {
        "text": {
            "file": "coverage.txt"
        },
        "html": {
            "subdir": "coverage"
        },
       "cobertura": {
           "subdir": "xml",
           "file": "coverage.xml"
       }
    }
}

For this configuration, the "text" reporter would output to "./reports/coverage.txt". The "html" reporter would output to "./reports/coverage/...". The "cobertura" reporter would output to "./reports/xml/coverage.xml". The watermarks would be [ 85, 90 ] for all four of the threshold settings. Here is an example of what that looks like in the "html" reporter output.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions