Skip to content

Commit

Permalink
adds ability to pass custom reporter options (#9572)
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart authored Feb 15, 2020
1 parent f47ade4 commit 9d1236b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,20 @@ 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\<string>]
### `coverageReporters` [array\<string | [string,any]>]

Default: `["json", "lcov", "text", "clover"]`

A list of reporter names that Jest uses when writing coverage reports. Any [istanbul reporter](https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-reports/lib) can be used.

_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`
Expand Down
21 changes: 21 additions & 0 deletions packages/jest-reporters/src/__tests__/coverage_reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jest
let libCoverage;
let libSourceMaps;
let CoverageReporter;
let istanbulReports;

import path from 'path';
import mock from 'mock-fs';
Expand All @@ -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'] = {
Expand Down Expand Up @@ -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();
});
});
});
9 changes: 8 additions & 1 deletion packages/jest-reporters/src/coverage_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type DefaultOptions = {
clearMocks: boolean;
collectCoverage: boolean;
coveragePathIgnorePatterns: Array<string>;
coverageReporters: Array<string>;
coverageReporters: Array<string | [string, any]>;
coverageProvider: CoverageProvider;
errorOnDeprecated: boolean;
expand: boolean;
Expand Down Expand Up @@ -241,7 +241,7 @@ export type GlobalConfig = {
coverageDirectory: string;
coveragePathIgnorePatterns?: Array<string>;
coverageProvider: CoverageProvider;
coverageReporters: Array<keyof ReportOptions>;
coverageReporters: Array<keyof ReportOptions | [keyof ReportOptions, any]>;
coverageThreshold?: CoverageThreshold;
detectLeaks: boolean;
detectOpenHandles: boolean;
Expand Down

0 comments on commit 9d1236b

Please sign in to comment.