Allow custom coverage reporters
See original GitHub issue🚀 Feature Proposal
Allow jest to use custom coverageReporters
.
Motivation
Only istanbuljs coverage reporters can be used so far. But istanbuljs has limited number of reporters which can’t cover all possible options.
E.g. I’ve needed a coverage reports to be in junit-xml
format which isn’t present in istanbuljs library.
Example
It could be used just like test reporters
.
// jest.config.js
{
collectCoverage: true,
coverageReporters: [require.resolve('<path to appropriate node_module>')],
...
}
//
{
collectCoverage: true,
coverageReporters: [
'json',
'text',
[
require.resolve('<path to appropriate node_module>'), { option: {}, ... }
] // with options
],
...
}
Workaround
Define custom test (not coverage) reporter. Implement onRunComplete
method and extract coverage results from aggregatedResult
.
class CustomCoverageReporter {
...
onRunComplete(context: jest.Context, aggregatedResult: jest.AggregatedResult): void {
// extract coverage here
}
}
module.exports = CustomCoverageReporter;
// jest.config.js
{
...
collectCoverage: true,
reporters: [require.resolve('<path to CustomCoverageReporter>')]
}
But you need be aware that it’ll be invoked even if collectCoverage
flag is set to false
. So additional validations are required.
Pitch
Why does this feature belong in the Jest core platform?
It would be great to be able to export coverage reports in different desirable formats. This enhancement would provide more flexibility.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:18
- Comments:5
Top GitHub Comments
I think it changed again and I haven’t worked on this a while. I guess this project does not help to understand internals or? https://github.com/dkelosky/jest-stare/blob/master/src/reporter/Reporter.ts
Hi! Could you please share link for example about how to receive the coverageMap?
I solved my case like this, but I think this is bad idea: