bcoe / c8

output coverage reports using Node.js' built in coverage
ISC License
2k stars 91 forks source link

Cannot add flags after the script #540

Open darcyrush opened 3 months ago

darcyrush commented 3 months ago

I am executing c8 like so;

c8 --src=src/ npm run test

Where npm run test is a custom defined npm command with its own parameters.

Sometimes I need to generate a report, so I do

c8 --src=src/ --reports-dir=coverage/ --reporter=text --reporter=lcov npm run test

All good.

Looking at help, it appears that c8 allows additional parameters after the script

$ npx c8 --help
c8 [opts] [script] [opts]

I tried to execute flags like so, but the flags after the npm run test script are ignored

c8 --src=src/ npm run test --reports-dir=coverage/ --reporter=text --reporter=lcov

How can I add the flags after the npm run test script?

ericmorand commented 3 months ago

Hey @darcyrush,

Can I assume that what you want to achieve is to have a cover script in package.json that is sort of pre-parameterized, and you'd like to be able to add some additional flags when you execute it?

Like:

{
  "scripts": {
   "cover": "c8 --src=src/ npm run test"
  }
}

And using it this way, to add the HTML reporter:

npm run cover -- --reporter=html

Is it what you are trying to achieve?

darcyrush commented 3 months ago

Is it what you are trying to achieve?

Yes exactly, but I need to test more to see if this is a c8 specific issue - this could be the effect of a different underlying library I am using within npm run test

ericmorand commented 3 months ago

Actually, this is an issue from npm itself, that has been around forever: it is not possible to decide where the additional arguments passed when running the script are injected. In your case, the additional arguments would need to be injected before "npm run test" so that they apply to C8 instead of "npm run test"…but with npm scripts there is no way to achieve such a thing.

The only solution would be to be able to reverse the way the script is executed, to have c8 as the last execute program of the script:

"cover": "npm run test | c8 --src=src/"

But this is not possible because c8 doesn't receive the result of the executed command: it actually executes the passed command, so there is no way to write it like above.

The actual solution would be to have c8 expose a dedicated command to run the passed command, and then another dedicated one to output the report. This is what tools like Coverage.py for Python or my own One Double Zero provide.

By having a dedicated command to run "npm run test", and some dedicated ones to emit the report and check, you can write your npm script this way:

"cover": "odz run npm run test && odz report --src=src/"

With this approach, the additional arguments passed after -- would apply to odz report, achieving what you need.