Skip to content

Commit

Permalink
Custom Reporters (merge/fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov committed Apr 22, 2017
1 parent d9541b0 commit 4d93360
Show file tree
Hide file tree
Showing 13 changed files with 849 additions and 180 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"jest.pathToJest": "npm run jest --",
"editor.rulers": [
80
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ Object {
"onRunComplete": Object {
"called": true,
"config": "object",
"numFailedTests": 1,
"numPassedTests": 0,
"numTotalTests": 1,
},
"onRunStart": Object {
"called": true,
Expand All @@ -29,7 +26,7 @@ Object {
"onTestStart": Object {
"called": true,
"config": false,
"path": true,
"path": false,
},
"options": Object {
"christop": "pojer",
Expand All @@ -44,9 +41,6 @@ Object {
"onRunComplete": Object {
"called": true,
"config": "object",
"numFailedTests": 0,
"numPassedTests": 1,
"numTotalTests": 1,
},
"onRunStart": Object {
"called": true,
Expand All @@ -60,7 +54,7 @@ Object {
"onTestStart": Object {
"called": true,
"config": false,
"path": true,
"path": false,
},
"options": Object {
"christop": "pojer",
Expand Down
16 changes: 6 additions & 10 deletions integration_tests/__tests__/custom-reporters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ describe('Custom Reporters Integration', () => {
});

test('TestReporter with all tests passing', () => {
const {
stdout,
status,
stderr,
} = runJest('custom_reporters', ['add-test.js']);
const {stdout, status, stderr} = runJest('custom_reporters', [
'add-test.js',
]);
const parsedJSON = JSON.parse(stdout);

expect(status).toBe(0);
Expand All @@ -72,11 +70,9 @@ describe('Custom Reporters Integration', () => {
});

test('TestReporter with all tests failing', () => {
const {
stdout,
status,
stderr,
} = runJest('custom_reporters', ['add-fail-test.js']);
const {stdout, status, stderr} = runJest('custom_reporters', [
'add-fail-test.js',
]);

const parsedJSON = JSON.parse(stdout);

Expand Down
11 changes: 11 additions & 0 deletions integration_tests/custom_reporters/__tests__/add-fail-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

const add = require('../add');

describe('CustomReporters', () => {
Expand Down
11 changes: 11 additions & 0 deletions integration_tests/custom_reporters/__tests__/add-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

const add = require('../add');

describe('Custom Reporters', () => {
Expand Down
11 changes: 11 additions & 0 deletions integration_tests/custom_reporters/add.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

module.exports = function add(x, y) {
return x + y;
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
* IncompleteReporter
* Reporter to test for the flexibility of the interface we implemented.
* The reporters shouldn't be required to implement all the methods
*
*
* This only implements one mehtod onRunComplete which should be called
*/
class IncompleteReporter {
constructor(options) {
this.options = {};
}

onRunComplete(config, results) {
onRunComplete(contexts, config, results) {
console.log('onRunComplete is called');
console.log('Passed Tests: ' + results.numPassedTests);
console.log('Failed Tests: ' + results.numFailedTests);
Expand Down
105 changes: 42 additions & 63 deletions packages/jest-cli/src/ReporterDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

'use strict';

import type {RunnerContext} from 'types/Reporters';
import type {HasteFS} from 'types/HasteMap';
import type {Config, Path} from 'types/Config';
import type {GlobalConfig} from 'types/Config';
import type {Context} from 'types/Context';
import type {Test} from 'types/TestRunner';
import type {TestResult, AggregatedResult} from 'types/TestResult';
import type BaseReporter from './reporters/BaseReporter';
import type {ReporterOnStartOptions} from 'types/Reporters';

export type RunOptions = {
estimatedTime: number,
Expand All @@ -22,88 +24,65 @@ export type RunOptions = {

class ReporterDispatcher {
_disabled: boolean;
_reporters: Array<Object>;
_runnerContext: RunnerContext;
_requiredMethods: Array<string>;
_reporters: Array<BaseReporter>;

constructor(hasteFS: HasteFS, getTestSummary: () => string) {
this._runnerContext = {getTestSummary, hasteFS};
constructor() {
this._reporters = [];
}

register(reporter: Object): void {
register(reporter: BaseReporter): void {
this._reporters.push(reporter);
}

unregister(ReporterClass: Function): void {
unregister(ReporterClass: Function) {
this._reporters = this._reporters.filter(
reporter => !(reporter instanceof ReporterClass),
);
}

onTestResult(
config: Config,
testResult: TestResult,
results: AggregatedResult,
) {
this._callReporterMethod('onTestResult', [
config,
testResult,
results,
this._runnerContext,
]);
}

onTestStart(config: Config, path: Path) {
this._callReporterMethod('onTestStart', [
config,
path,
this._runnerContext,
]);
onTestResult(test: Test, testResult: TestResult, results: AggregatedResult) {
this._reporters.forEach(
reporter =>
reporter.onTestResult &&
reporter.onTestResult(test, testResult, results),
);
}

onRunStart(config: Config, results: AggregatedResult, options: RunOptions) {
this._callReporterMethod('onRunStart', [
config,
results,
this._runnerContext,
options,
]);
onTestStart(test: Test) {
this._reporters.forEach(
reporter => reporter.onTestStart && reporter.onTestStart(test),
);
}

onRunComplete(config: Config, results: AggregatedResult) {
this._callReporterMethod('onRunComplete', [
config,
results,
this._runnerContext,
]);
onRunStart(
config: GlobalConfig,
results: AggregatedResult,
options: ReporterOnStartOptions,
) {
this._reporters.forEach(
reporter =>
reporter.onRunStart && reporter.onRunStart(config, results, options),
);
}

/**
* Helper mehtod to call only the methods that exist
* on a given reporter
*
* @private
* @param {string} method name of the mehtod to be called
* @param {Array<any>} reporterArgs arguments passed in to call the reporter
*/
_callReporterMethod(method: string, reporterArgs: Array<any>) {
this._reporters.forEach(reporter => {
if (reporter[method]) {
reporter[method].apply(reporter, reporterArgs);
}
});
onRunComplete(
contexts: Set<Context>,
config: GlobalConfig,
results: AggregatedResult,
) {
this._reporters.forEach(
reporter =>
reporter.onRunComplete &&
reporter.onRunComplete(contexts, config, results),
);
}

// Return a list of last errors for every reporter
getErrors(): Array<Error> {
return this._reporters.reduce(
(list, reporter) => {
const error = reporter.getLastError && reporter.getLastError();
return error ? list.concat(error) : list;
},
[],
);
return this._reporters.reduce((list, reporter) => {
const error = reporter.getLastError && reporter.getLastError();
return error ? list.concat(error) : list;
}, []);
}

hasErrors(): boolean {
Expand Down
22 changes: 2 additions & 20 deletions packages/jest-cli/src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
} from 'types/TestResult';
import type {Config, GlobalConfig, ReporterConfig} from 'types/Config';
import type {Context} from 'types/Context';
import type {Context} from 'types/Context';
import type {PathPattern} from './SearchSource';
import type {Test, Tests} from 'types/TestRunner';

Expand Down Expand Up @@ -276,18 +275,10 @@ class TestRunner {
return Promise.race([runAllTests, onInterrupt]).then(cleanup, cleanup);
}

/**
* Checks if default reporters should be added or not
* @private
*/
_shouldAddDefaultReporters(reporters?: Array<ReporterConfig>): boolean {
return !reporters || reporters.indexOf(DEFAULT_REPORTER_LABEL) !== -1;
}

/**
* Main method to Setup reporters to be used with TestRunner
* @private
*/
_setupReporters() {
const config = this._config;
const reporters = config.reporters;
Expand All @@ -308,24 +299,19 @@ class TestRunner {
this.addReporter(new CoverageReporter());
}


if (config.notify) {
this.addReporter(new NotifyReporter(this._options.startRun));
}
}

_setupDefaultReporters(config: Config) {
_setupDefaultReporters(config: GlobalConfig) {
this.addReporter(
config.verbose ? new VerboseReporter(config) : new DefaultReporter(),
);

this.addReporter(new SummaryReporter(this._options));
}

/**
* Adds Custom reporters to Jest
* @private
*/
_addCustomReporters(reporters: Array<ReporterConfig>) {
const customReporter = reporters.filter(reporter => reporter !== 'default');

Expand All @@ -348,11 +334,7 @@ class TestRunner {

/**
* Get properties of a reporter in an object
* to make dealing with them less painful
*
* Objects contain the following properties:
* - options
* - path
* to make dealing with them less painful.
*/
_getReporterProps(reporter: ReporterConfig): Object {
let props = {};
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-cli/src/reporters/VerboseReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
'use strict';

import type {GlobalConfig} from 'types/Config';
import type {
AggregatedResult,
AssertionResult,
Expand All @@ -22,9 +23,9 @@ const chalk = require('chalk');
const {ICONS} = require('../constants');

class VerboseReporter extends DefaultReporter {
_options: Config;
_options: GlobalConfig;

constructor(options: Config) {
constructor(options: GlobalConfig) {
super();
this._options = options;
}
Expand Down
Loading

0 comments on commit 4d93360

Please sign in to comment.