How to collect code coverage for spawned sub-processes using Jest?
Asked Answered
C

0

6

I am writing integration tests for a CLI. All the unit tests are written using Jest as out-of-the-box it produces code coverage without any configuration, but unfortunately it does not instrument sub-processes, for example executed via Node's spawn and fork commands.

I have tried to introduce nyc into the mix as suggested in this comment on a GitHub issue however it has not worked for me.

I have played with various configurations (based on the initial aforementioned suggestion and also ideas from these issues: 1, 2), however either I get no coverage statistics at all or only coverage for my unit tests, not the integration tests that spawn sub-processes.

The relevant parts of my package.json which configures nyc and Jest:

"scripts": {
  "test": "cross-env NODE_ENV=test nyc --clean jest --coverage",
},
"jest": {
  "verbose": true,
  "testURL": "http://localhost/",
  "globalSetup": "./jest.setup.js",
  "transform": {
    "^.+\\.js$": "babel-jest"
  },
  "collectCoverage": false
},
"nyc": {
  "include": [
    "packages/*/src/**/*.js"
  ],
  "reporter": [
    "html"
  ]
},

I am using execa to run the sub-processes and do so as follows:

await execa("nyc --reporter none node", args);
Carmencarmena answered 19/11, 2018 at 11:55 Comment(1)
Looks like you have "collectCoverage": false, but the linked Github comment says to use "collectCoverage": true, "coverageReporters": [ "none" ]Survey

© 2022 - 2024 — McMap. All rights reserved.