nyc icon indicating copy to clipboard operation
nyc copied to clipboard

Pass through "file" option to reporters

Open miyasudokoro opened this issue 4 years ago • 5 comments

Expected Behavior

I can pass through the "file" option to reporters such as Cobertura in order to specify the report filename.

Observed Behavior

I cannot specify the report filename.

The following lines of code in nyc/index pass through only four options to each reporter:

// nyc/index.js
    this.reporter.forEach((_reporter) => {
      reports.create(_reporter, {
        skipEmpty: this.config.skipEmpty,
        skipFull: this.config.skipFull,
        projectRoot: this.cwd,
        maxCols: process.stdout.columns || 100
      }).execute(context)
    })

However, the reporters are configured to expect potentially more options than these. Here is an example from Cobertura:

// cobertura.index
    constructor(opts) {
        super();

        this.cw = null;
        this.xml = null;
        this.projectRoot = opts.projectRoot || process.cwd();
        this.file = opts.file || 'cobertura-coverage.xml';
    }

As you can see, the capability to specify the filename is already present via the option "file." However, there is no way to pass it through. I would expect it to look something like this:

// nyc/index.js
    this.reporter.forEach((_reporter) => {
      reports.create(_reporter, {
        skipEmpty: this.config.skipEmpty,
        skipFull: this.config.skipFull,
        projectRoot: this.cwd,
        maxCols: process.stdout.columns || 100,
        file: this.config.reportFilename   <-- added line here
      }).execute(context)
    })

Environment Information

  System:
    OS: Windows 10 10.0.18362
    CPU: (8) x64 Intel(R) Core(TM) i7 CPU         880  @ 3.07GHz
    Memory: 7.40 GB / 15.96 GB
  Binaries:
    Node: 12.14.0 - C:\Program Files\nodejs\node.EXE
    npm: 6.13.4 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    nyc: ^15.0.0 => 15.0.0


miyasudokoro avatar Mar 25 '20 23:03 miyasudokoro

I'd like a way to do this, too, in my case to add "statements" to the metricsToShow option for the html-spa reporter, see https://github.com/istanbuljs/istanbuljs/issues/596

danvk avatar Feb 09 '21 17:02 danvk

As it turns out, it's not too hard to replicate what nyc report does in a script of your own and pass through the options you want. Here's my script to get statement coverage with the html-spa reporter, @miyasudokoro presumably you could do something similar for the cobertura reporter and the file option.

(This is in TypeScript, you'll have to compile to JS or run with ts-node to use it.)

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

process.env.NYC_CWD = process.cwd();
const nyc = new NYC(process.argv);

(async () => {
  const {config} = nyc;
  const context = libReport.createContext({
    dir: nyc.reportDirectory(),
    watermarks: config.watermarks,
    coverageMap: await nyc.getCoverageMapFromAllCoverageFiles(),
  });

  (reports.create('html-spa', {
    skipEmpty: config.skipEmpty,
    metricsToShow: ['statements', 'lines', 'branches', 'functions'],
  }) as any).execute(context);
})().catch(e => {
  // eslint-disable-next-line no-console
  console.error(e);
});

danvk avatar Feb 11 '21 16:02 danvk

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Apr 16 '22 12:04 stale[bot]

Yes, this is still relevant.

danvk avatar Apr 16 '22 14:04 danvk

I would love it if the entire configuration (this.config) was passed to the reporter.

electrovir avatar Dec 01 '22 23:12 electrovir