Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage reporter configuration improvements #2806

Open
miyasudokoro opened this issue Sep 16, 2024 · 0 comments
Open

Coverage reporter configuration improvements #2806

miyasudokoro opened this issue Sep 16, 2024 · 0 comments

Comments

@miyasudokoro
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant