diff --git a/CHANGELOG.md b/CHANGELOG.md index 5feee595060a..6cb6fe06d920 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -123,6 +123,7 @@ - `[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)) - `[jest-util]` `console.timeEnd` now properly log elapsed time in milliseconds. ([#6456](https://github.com/facebook/jest/pull/6456)) - `[jest-mock]` Fix `MockNativeMethods` access in react-native `jest.mock()` ([#6505](https://github.com/facebook/jest/pull/6505)) +- `[jest-cli]` Fix `reporters` for `moduleName` = `'default'` ([#6542](https://github.com/facebook/jest/pull/6542)) ### Chore & Maintenance diff --git a/packages/jest-cli/src/TestScheduler.js b/packages/jest-cli/src/TestScheduler.js index f2e678e8ad61..7446d20b8ac6 100644 --- a/packages/jest-cli/src/TestScheduler.js +++ b/packages/jest-cli/src/TestScheduler.js @@ -255,10 +255,14 @@ export default class TestScheduler { } } - _shouldAddDefaultReporters(reporters?: Array): boolean { + _shouldAddDefaultReporters( + reporters?: Array, + ): boolean { return ( !reporters || - !!reporters.find(reporterConfig => reporterConfig[0] === 'default') + !!reporters.find( + reporter => this._getReporterProps(reporter).path === 'default', + ) ); } @@ -303,14 +307,12 @@ export default class TestScheduler { this.addReporter(new SummaryReporter(this._globalConfig)); } - _addCustomReporters(reporters: Array) { - const customReporters = reporters.filter( - reporterConfig => reporterConfig[0] !== 'default', - ); - - customReporters.forEach((reporter, index) => { + _addCustomReporters(reporters: Array) { + reporters.forEach((reporter, index) => { const {options, path} = this._getReporterProps(reporter); + if (path === 'default') return; + try { // $FlowFixMe const Reporter = require(path); @@ -331,7 +333,7 @@ export default class TestScheduler { * to make dealing with them less painful. */ _getReporterProps( - reporter: ReporterConfig, + reporter: string | ReporterConfig, ): {path: string, options?: Object} { if (typeof reporter === 'string') { return {options: this._options, path: reporter}; diff --git a/packages/jest-cli/src/__tests__/TestScheduler.test.js b/packages/jest-cli/src/__tests__/TestScheduler.test.js index b3d7bdefe614..e700d4d17aa4 100644 --- a/packages/jest-cli/src/__tests__/TestScheduler.test.js +++ b/packages/jest-cli/src/__tests__/TestScheduler.test.js @@ -27,6 +27,45 @@ jest.mock('jest-runner-parallel', () => jest.fn(() => mockParallelRunner), { virtual: true, }); +test('config for reporters supports `default`', () => { + const undefinedReportersScheduler = new TestScheduler( + { + reporters: undefined, + }, + {}, + ); + const numberOfReporters = + undefinedReportersScheduler._dispatcher._reporters.length; + + const stringDefaultReportersScheduler = new TestScheduler( + { + reporters: ['default'], + }, + {}, + ); + expect(stringDefaultReportersScheduler._dispatcher._reporters.length).toBe( + numberOfReporters, + ); + + const defaultReportersScheduler = new TestScheduler( + { + reporters: [['default', {}]], + }, + {}, + ); + expect(defaultReportersScheduler._dispatcher._reporters.length).toBe( + numberOfReporters, + ); + + const emptyReportersScheduler = new TestScheduler( + { + reporters: [], + }, + {}, + ); + expect(emptyReportersScheduler._dispatcher._reporters.length).toBe(0); +}); + test('.addReporter() .removeReporter()', () => { const scheduler = new TestScheduler({}, {}); const reporter = new SummaryReporter(); diff --git a/types/Config.js b/types/Config.js index 974b9f180baf..f7270b049a64 100644 --- a/types/Config.js +++ b/types/Config.js @@ -116,7 +116,7 @@ export type InitialOptions = { globalSetup?: ?string, globalTeardown?: ?string, haste?: HasteConfig, - reporters?: Array, + reporters?: Array, logHeapUsage?: boolean, lastCommit?: boolean, listTests?: boolean, @@ -219,7 +219,7 @@ export type GlobalConfig = {| passWithNoTests: boolean, projects: Array, replname: ?string, - reporters: Array, + reporters: Array, runTestsByPath: boolean, rootDir: Path, silent: boolean,