Skip to content

Commit 9737621

Browse files
artolaaaronabramov
authored andcommitted
Fix reporters' config (#6542)
* Fix `reporters` to handle string params Documentation shows reporters as `reporters [array<moduleName | [moduleName, options]>]`, the code was incorrectly handling the following cases: ``` // no `string` moduleName // _addCustomReporters(reporters: Array<ReporterConfig>) reporters: ['default', 'someother'] // _shouldAddDefaultReporters(reporters?: Array<ReporterConfig>): boolean { reporters: [['default', {}], 'someother'] ``` This fix, use the already existing `_getReporterProps` to compare `{path} === 'default'`. * Update CHANGELOG.md * Update Config.js * prettier code * prettier code * regression test - config for reporters supports `default` * Update CHANGELOG.md
1 parent 2736b9b commit 9737621

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
- `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([#6398](https://github.com/facebook/jest/pull/6398))
128128
- `[jest-util]` `console.timeEnd` now properly log elapsed time in milliseconds. ([#6456](https://github.com/facebook/jest/pull/6456))
129129
- `[jest-mock]` Fix `MockNativeMethods` access in react-native `jest.mock()` ([#6505](https://github.com/facebook/jest/pull/6505))
130+
- `[jest-cli]` Fix `reporters` for `moduleName` = `'default'` ([#6542](https://github.com/facebook/jest/pull/6542))
130131

131132
### Chore & Maintenance
132133

packages/jest-cli/src/TestScheduler.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,14 @@ export default class TestScheduler {
255255
}
256256
}
257257

258-
_shouldAddDefaultReporters(reporters?: Array<ReporterConfig>): boolean {
258+
_shouldAddDefaultReporters(
259+
reporters?: Array<string | ReporterConfig>,
260+
): boolean {
259261
return (
260262
!reporters ||
261-
!!reporters.find(reporterConfig => reporterConfig[0] === 'default')
263+
!!reporters.find(
264+
reporter => this._getReporterProps(reporter).path === 'default',
265+
)
262266
);
263267
}
264268

@@ -303,14 +307,12 @@ export default class TestScheduler {
303307
this.addReporter(new SummaryReporter(this._globalConfig));
304308
}
305309

306-
_addCustomReporters(reporters: Array<ReporterConfig>) {
307-
const customReporters = reporters.filter(
308-
reporterConfig => reporterConfig[0] !== 'default',
309-
);
310-
311-
customReporters.forEach((reporter, index) => {
310+
_addCustomReporters(reporters: Array<string | ReporterConfig>) {
311+
reporters.forEach((reporter, index) => {
312312
const {options, path} = this._getReporterProps(reporter);
313313

314+
if (path === 'default') return;
315+
314316
try {
315317
// $FlowFixMe
316318
const Reporter = require(path);
@@ -331,7 +333,7 @@ export default class TestScheduler {
331333
* to make dealing with them less painful.
332334
*/
333335
_getReporterProps(
334-
reporter: ReporterConfig,
336+
reporter: string | ReporterConfig,
335337
): {path: string, options?: Object} {
336338
if (typeof reporter === 'string') {
337339
return {options: this._options, path: reporter};

packages/jest-cli/src/__tests__/TestScheduler.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,45 @@ jest.mock('jest-runner-parallel', () => jest.fn(() => mockParallelRunner), {
2727
virtual: true,
2828
});
2929

30+
test('config for reporters supports `default`', () => {
31+
const undefinedReportersScheduler = new TestScheduler(
32+
{
33+
reporters: undefined,
34+
},
35+
{},
36+
);
37+
const numberOfReporters =
38+
undefinedReportersScheduler._dispatcher._reporters.length;
39+
40+
const stringDefaultReportersScheduler = new TestScheduler(
41+
{
42+
reporters: ['default'],
43+
},
44+
{},
45+
);
46+
expect(stringDefaultReportersScheduler._dispatcher._reporters.length).toBe(
47+
numberOfReporters,
48+
);
49+
50+
const defaultReportersScheduler = new TestScheduler(
51+
{
52+
reporters: [['default', {}]],
53+
},
54+
{},
55+
);
56+
expect(defaultReportersScheduler._dispatcher._reporters.length).toBe(
57+
numberOfReporters,
58+
);
59+
60+
const emptyReportersScheduler = new TestScheduler(
61+
{
62+
reporters: [],
63+
},
64+
{},
65+
);
66+
expect(emptyReportersScheduler._dispatcher._reporters.length).toBe(0);
67+
});
68+
3069
test('.addReporter() .removeReporter()', () => {
3170
const scheduler = new TestScheduler({}, {});
3271
const reporter = new SummaryReporter();

types/Config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export type InitialOptions = {
116116
globalSetup?: ?string,
117117
globalTeardown?: ?string,
118118
haste?: HasteConfig,
119-
reporters?: Array<ReporterConfig | string>,
119+
reporters?: Array<string | ReporterConfig>,
120120
logHeapUsage?: boolean,
121121
lastCommit?: boolean,
122122
listTests?: boolean,
@@ -219,7 +219,7 @@ export type GlobalConfig = {|
219219
passWithNoTests: boolean,
220220
projects: Array<Glob>,
221221
replname: ?string,
222-
reporters: Array<ReporterConfig>,
222+
reporters: Array<string | ReporterConfig>,
223223
runTestsByPath: boolean,
224224
rootDir: Path,
225225
silent: boolean,

0 commit comments

Comments
 (0)