Jest: Code coverage of spawned processes

Created on 23 Mar 2017  路  9Comments  路  Source: facebook/jest

Do you want to request a feature or report a bug?

Unclear, hopefully just a bug?

What is the current behavior?

Any node.js code run via the child_process module doesn't display any coverage information. .spawn() vs .fork() doesn't seem to matter.

https://repl.it/G9jH/0

What is the expected behavior?

I'd like to see coverage numbers from spawned node processes assuming that they were part of the project.

Windows

Most helpful comment

I found a workaround for this:

  • Instead of jest run nyc jest.
  • Configure jest to collect coverage but don't report it:
    "collectCoverage": true, "coverageReporters": [ "none" ]
  • When spawning child processes wrap them in a nyc call:
    like nyc --reporter none node child.js instead of node child.js
  • Configure nyc like you want to.

All 9 comments

unfortunately it's not possible now because that will be very very hard to implement.

the coverage is generated by instrumenting the code that's being run with runtime coverage checks + exporting the results after the process is finished. This process is not generic at all, and since pretty much anything can be run as a child process, there's no possible way we can collect coverage from these processes.

Interesting, I assumed jest used nyc for coverage and that's able to provide coverage of spawned processes.

yeah, it's definitely possible and nyc is very generic and independent to do this. But in Jest i think we're a little far from it.
Our transformation (and instrumentation) logic heavily depends on jest environment and test configuration. It provides a lot of benefits, but also makes it hard to work on things like this one

Closing this as wontfix for now, it is unlikely we'll get to this.

I found a workaround for this:

  • Instead of jest run nyc jest.
  • Configure jest to collect coverage but don't report it:
    "collectCoverage": true, "coverageReporters": [ "none" ]
  • When spawning child processes wrap them in a nyc call:
    like nyc --reporter none node child.js instead of node child.js
  • Configure nyc like you want to.

@sokra nice, that's really gross but better than nothing!

@aaronabramov see the tmp project where we use external process for integration testing. There, we use istanbul and mocha and collect the coverage data from the spawned child processes using https://github.com/raszi/node-tmp/blob/master/test/child-process.js#L40.
Later on we just run istanbul reports in https://github.com/raszi/node-tmp/blob/master/package.json#L41.
Perhaps this would be a possible solution for you?

Since this is still seeing some activity, this is the open issue tracking this: #5274 (we're not any closer to a solution, but that's where discussion should happen)

I only had to run nyc jest instead of jest --coverage. No further changes were required.

Was this page helpful?
0 / 5 - 0 ratings