diff --git a/CHANGELOG.md b/CHANGELOG.md index eff713ac0b7a..2b556336e00c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[jest-runtime]` Override `module.createRequire` to return a Jest-compatible `require` function ([#9469](https://github.com/facebook/jest/pull/9469)) - `[*]` Support array of paths for `moduleNameMapper` aliases ([#9465](https://github.com/facebook/jest/pull/9465)) +- `[jest-reporters]` Adds ability to pass options to the istanbul-reporter through `coverageReporters` ([#9572](https://github.com/facebook/jest/pull/9572)) ### Fixes diff --git a/docs/Configuration.md b/docs/Configuration.md index a345afe2f3c7..5339a82b1c5a 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -195,7 +195,7 @@ Note that using `v8` is considered experimental. This uses V8's builtin code cov 1. Tests needs to run in Node test environment (support for `jsdom` requires [`jest-environment-jsdom-sixteen`](https://www.npmjs.com/package/jest-environment-jsdom-sixteen)) 1. V8 has way better data in the later versions, so using the latest versions of node (v13 at the time of this writing) will yield better results -### `coverageReporters` [array\] +### `coverageReporters` [array\] Default: `["json", "lcov", "text", "clover"]` @@ -203,6 +203,12 @@ A list of reporter names that Jest uses when writing coverage reports. Any [ista _Note: Setting this option overwrites the default values. Add `"text"` or `"text-summary"` to see a coverage summary in the console output._ +_Note: You can pass additional options to the istanbul reporter using the tuple form. For example:_ + +```json +["json", ["lcov", {"projectRoot": "../../"}]] +``` + ### `coverageThreshold` [object] Default: `undefined` diff --git a/packages/jest-reporters/src/__tests__/coverage_reporter.test.js b/packages/jest-reporters/src/__tests__/coverage_reporter.test.js index 491a52173711..7e1cf5c4ceb6 100644 --- a/packages/jest-reporters/src/__tests__/coverage_reporter.test.js +++ b/packages/jest-reporters/src/__tests__/coverage_reporter.test.js @@ -20,6 +20,7 @@ jest let libCoverage; let libSourceMaps; let CoverageReporter; +let istanbulReports; import path from 'path'; import mock from 'mock-fs'; @@ -28,6 +29,7 @@ beforeEach(() => { CoverageReporter = require('../coverage_reporter').default; libCoverage = require('istanbul-lib-coverage'); libSourceMaps = require('istanbul-lib-source-maps'); + istanbulReports = require('istanbul-reports'); const fileTree = {}; fileTree[process.cwd() + '/path-test-files'] = { @@ -417,4 +419,23 @@ describe('onRunComplete', () => { expect(testReporter.getLastError()).toBeUndefined(); }); }); + + test(`that it passes custom options when creating reporters`, () => { + const testReporter = new CoverageReporter({ + coverageReporters: ['json', ['lcov', {maxCols: 10, projectRoot: './'}]], + }); + testReporter.log = jest.fn(); + return testReporter + .onRunComplete(new Set(), {}, mockAggResults) + .then(() => { + expect(istanbulReports.create).toHaveBeenCalledWith('json', { + maxCols: process.stdout.columns || Infinity, + }); + expect(istanbulReports.create).toHaveBeenCalledWith('lcov', { + maxCols: 10, + projectRoot: './', + }); + expect(testReporter.getLastError()).toBeUndefined(); + }); + }); }); diff --git a/packages/jest-reporters/src/coverage_reporter.ts b/packages/jest-reporters/src/coverage_reporter.ts index 1da07f073b76..234f632643b6 100644 --- a/packages/jest-reporters/src/coverage_reporter.ts +++ b/packages/jest-reporters/src/coverage_reporter.ts @@ -104,8 +104,15 @@ export default class CoverageReporter extends BaseReporter { coverageReporters.push('text-summary'); } coverageReporters.forEach(reporter => { + let additionalOptions = {}; + if (Array.isArray(reporter)) { + [reporter, additionalOptions] = reporter; + } istanbulReports - .create(reporter, {maxCols: process.stdout.columns || Infinity}) + .create(reporter, { + maxCols: process.stdout.columns || Infinity, + ...additionalOptions, + }) // @ts-ignore .execute(reportContext); }); diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index b16cc1c71d46..5bb9eea84b11 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -39,7 +39,7 @@ export type DefaultOptions = { clearMocks: boolean; collectCoverage: boolean; coveragePathIgnorePatterns: Array; - coverageReporters: Array; + coverageReporters: Array; coverageProvider: CoverageProvider; errorOnDeprecated: boolean; expand: boolean; @@ -241,7 +241,7 @@ export type GlobalConfig = { coverageDirectory: string; coveragePathIgnorePatterns?: Array; coverageProvider: CoverageProvider; - coverageReporters: Array; + coverageReporters: Array; coverageThreshold?: CoverageThreshold; detectLeaks: boolean; detectOpenHandles: boolean;