Skip to content

Instantly share code, notes, and snippets.

@igrek8
Last active December 6, 2019 12:27
Show Gist options
  • Save igrek8/9833300707d21d776b36069c36ca22ed to your computer and use it in GitHub Desktop.
Save igrek8/9833300707d21d776b36069c36ca22ed to your computer and use it in GitHub Desktop.
Jest get coverage programmatically (gitlab pipelines)
node getCoverage.js

=>

CoverageSummary {
  data: {
    lines: { total: 10, covered: 10, skipped: 0, pct: 10.0 },
    statements: { total: 10, covered: 10, skipped: 0, pct: 10.0 },
    functions: { total: 10, covered: 10, skipped: 0, pct: 10.0 },
    branches: { total: 10, covered: 10, skipped: 0, pct: 10.0 }
  }
}
const jest = require('jest');
const config = require('jest-config');
const projects = ['.'];
jest.runCLI(config, projects).then((res) => console.log(res.results.coverage));
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html
module.exports = {
testResultsProcessor: '<rootDir>/jest.testResultsProcessor.js',
};
const reporter = require('istanbul-lib-coverage');
module.exports = function testResultsProcessor(options) {
const coverage = reporter.createCoverageSummary();
const coverageMap = reporter.createCoverageMap(options.coverageMap);
coverageMap.files().forEach((file) => coverage.merge(coverageMap.fileCoverageFor(file).toSummary()));
return { ...options, coverage };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment