bcoe / c8

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

Watching child process #421

Open buksy90 opened 2 years ago

buksy90 commented 2 years ago

Hi guys,

I have a project with Vue.js + Typescript + Mocha that I need to generate code coverage from. I was using nyc + istanbul before, but had the issue with ESM modules, so I switched to c8 but I have an issue that when running tests, there are several child processes started and c8 lost track of it.

Vue-cli is triggering new process with mochapack which triggers new process with webpack which triggers new process with typescript and mocha (at least this is how I understand it :)).

I tried to exclude as many process from this chain as possible and ended with following command:

cross-env MODE=test NODE_ENV=test c8 ./node_modules/.bin/mochapack --recursive --require ./node_modules/@vue/cli-plugin-unit-mocha/setup.js --webpack-config ./node_modules/@vue/cli-service/webpack.confi
g.js 'src/**/analytics.spec.ts'

This runs c8 that runs mochapack that runs webpack that runs typescript and then mocha. In the coverage output generated by c8 I can see my config files that are used by webpack being stated with coverage included but no test files. Clearly test files are run by child process and therefore not shown in output.

TLDR: Is there any way to watch for child processes with c8 too? Or are you aware of any other solution (i.e command webpack to run c8 before running typescript & mocha)?

Thanks :)

bcoe commented 2 years ago

Answering the questions:

Is there any way to watch for child processes with c8 too?

c8 will automatically pass the NODE_V8_COVERAGE flag to subprocesses, which is what is used to collect coverage:

https://nodejs.org/api/cli.html#node_v8_coveragedir

This is in turn passed to subprocesses.

Or are you aware of any other solution (i.e command webpack to run c8 before running typescript & mocha)?

I would suggest trying to set NODE_V8_COVERAGE in your test environment, and eliminate c8 form the equation. It will then write coverage output to a directory. You can confirm whether or not it's been set by checking process.env in your test environment.