question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 4 years ago
  • Reactions:18
  • Comments:5

github_iconTop GitHub Comments

1reaction
valoricDecommented, Oct 19, 2022

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

0reactions
YozhEzhicommented, Oct 19, 2022

I think this one can be closed. Istanbul changed the way they integrate the reporter. You just need to export a class with a function execute(results) and then you receive the coverageMap

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:

import libCoverage from 'istanbul-lib-coverage';
import { CoverageReporter } from '@jest/reporters';

class Reporter extends CoverageReporter {
    async onRunComplete(): Promise<void> {
        // @ts-ignore
        const { map } = await super._getCoverageResult();
        if (!map) return;

        const coverageMap = libCoverage.createCoverageMap(map);
       // ...
       }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
A list of reporter names that Jest uses when writing coverage reports. ... This allows for a custom configuration of the background color...
Read more >
How to use Cobertura and JUnit reporters with LWC Jest?
To use Cobertura or other coverage reporters and formats you just need to add the following line to the existing Jest configuration file:...
Read more >
Coverage reporter is asking for `lcov.info` file if `coverage` is ...
Coverage reporter is asking for `lcov.info` file if `coverage` is absent in ... 'test/tools/custom.matchers.js'}, {pattern: 'test/tools/jasmine.ext.js'}, ...
Read more >
Reporters | Cypress Documentation
Reporter Options. Some reporters accept options that customize their behavior. These can be specified in your Cypress configuration or via command line options....
Read more >
Enable code coverage for Jest tests in jest-html-reporter/Istanbul
Html report for the test can be generated by Istanbul by simply configuring the package.json file since it comes automatically integrated ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found