Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds ability to pass custom reporter options #9572

Merged
merged 5 commits into from Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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: process.stdout.columns || Infinity,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be called with 10 no? I think it makes sense for user options to always override Jest's defaults

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense for user options to always override Jest's defaults

I wasn't sure what your stance would be about this option, I'll change it

projectRoot: './',
});
expect(testReporter.getLastError()).toBeUndefined();
});
});
});
9 changes: 8 additions & 1 deletion packages/jest-reporters/src/coverage_reporter.ts
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, {
...additionalOptions,
maxCols: process.stdout.columns || Infinity,
})
// @ts-ignore
.execute(reportContext);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-types/src/Config.ts
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