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

Cannot add flags after the script #540

Open
darcyrush opened this issue Jul 16, 2024 · 3 comments
Open

Cannot add flags after the script #540

darcyrush opened this issue Jul 16, 2024 · 3 comments

Comments

@darcyrush
Copy link

darcyrush commented Jul 16, 2024

  • Version: v22.4.0
  • Platform: Linux M720q 6.5.0-35-generic #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 7 09:00:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

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
Copy link

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
Copy link
Author

darcyrush commented Jul 26, 2024

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
Copy link

ericmorand commented Jul 26, 2024

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.

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

2 participants