Skip to content

cenfun/jest-monocart-coverage

Repository files navigation

jest-monocart-coverage

A Jest custom reporter for monocart coverage reports

Install

npm i jest-monocart-coverage

Usage

// jest.config.js
const config = {

    // Enable coverage
    collectCoverage: true,
    // Recommended to use `v8` to support the generation of native v8 coverage reports.
    coverageProvider: 'v8',
    // Monocart can also support all coverage reports, so there is no need to set up reports here.
    coverageReporters: ['none'],

    reporters: [
        // If custom reporters are specified, the default Jest reporter will be overridden. If you wish to keep it, 'default' must be passed as a reporters name:
        'default',

        // Monocart custom reporter to generate coverage reports. 
        ['jest-monocart-coverage', {
            name: 'My Unit Coverage Report',
            reports: [
                ['v8'],
                ['console-summary'],
                ['lcovonly']
            ],
            outputDir: './coverage-reports/unit'
        }]
    ]
    
};
export default config;

Check monocart coverage reports for more coverage options.

The Execution Order of Hooks

  • For Jest: custom reporter onEnd -> globalTeardown
  • For Playwright: globalTeardown -> coverage onEnd -> custom reporter onEnd

Common issues

  • Resolve full path of the file with option sourcePath (issue #5)
const path = require("path")

// MCR coverage options
{

    sourcePath: (filePath, info)=> {
        if (!filePath.includes('/') && info.distFile) {
            return `${path.dirname(info.distFile)}/${filePath}`;
        }
        return filePath;
    }
  
}
  • Failed to load Sourcemap (issue #6)

Using sourceMap instead of inlineSourceMap in tsconfig.json

{
    "compilerOptions": {
        "sourceMap": true,
        "inlineSourceMap": false
    }
}