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 May 2, 2017
1 parent eb0d495 commit 8dec8ba
Show file tree
Hide file tree
Showing 24 changed files with 913 additions and 331 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 @@ -12,14 +12,12 @@ exports[`Custom Reporters Integration TestReporter with all tests failing 1`] =
Object {
"onRunComplete": Object {
"called": true,
"config": "object",
"numFailedTests": 1,
"numPassedTests": 0,
"numTotalTests": 1,
},
"onRunStart": Object {
"called": true,
"config": "object",
"options": "object",
},
"onTestResult": Object {
Expand All @@ -28,11 +26,10 @@ Object {
},
"onTestStart": Object {
"called": true,
"config": false,
"path": true,
"path": false,
},
"options": Object {
"christop": "pojer",
"christoph": "pojer",
"dmitrii": "abramov",
"hello": "world",
},
Expand All @@ -43,14 +40,12 @@ exports[`Custom Reporters Integration TestReporter with all tests passing 1`] =
Object {
"onRunComplete": Object {
"called": true,
"config": "object",
"numFailedTests": 0,
"numPassedTests": 1,
"numTotalTests": 1,
},
"onRunStart": Object {
"called": true,
"config": "object",
"options": "object",
},
"onTestResult": Object {
Expand All @@ -59,11 +54,10 @@ Object {
},
"onTestStart": Object {
"called": true,
"config": false,
"path": true,
"path": false,
},
"options": Object {
"christop": "pojer",
"christoph": "pojer",
"dmitrii": "abramov",
"hello": "world",
},
Expand All @@ -78,6 +72,8 @@ Unexpected value for Path at index 0 of reporter at index 0
string
Got:
number
Reporters config:
[ 3243242 ]
Configuration Documentation:
https://facebook.github.io/jest/docs/configuration.html
Expand Down
18 changes: 8 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,10 @@ 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 +71,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 All @@ -87,6 +84,7 @@ describe('Custom Reporters Integration', () => {

test('IncompleteReporter for flexibility', () => {
const {stderr, stdout, status} = runJest('custom_reporters', [
'--no-cache',
'--config',
JSON.stringify({
reporters: ['<rootDir>/reporters/IncompleteReporter.js'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

throw new Error(
`this error should not be a problem because` +
`this file is never required or executed`,
`this file is never required or executed`
);

// Flow annotations to make sure istanbul can instrument non ES6 source
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
15 changes: 12 additions & 3 deletions integration_tests/custom_reporters/add.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module.exports = function add(x, y) {
return x + y;
};
/**
* 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 = (x, y) => x + y;
2 changes: 1 addition & 1 deletion integration_tests/custom_reporters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
["<rootDir>/reporters/TestReporter.js", {
"hello": "world",
"dmitrii": "abramov",
"christop": "pojer"
"christoph": "pojer"
}]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
* 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
*
* This only implements one method onRunComplete which should be called
*/
class IncompleteReporter {
constructor(options) {
this.options = {};
}

onRunComplete(config, results) {
onRunComplete(contexts, results) {
console.log('onRunComplete is called');
console.log('Passed Tests: ' + results.numPassedTests);
console.log('Failed Tests: ' + results.numFailedTests);
Expand Down
19 changes: 8 additions & 11 deletions integration_tests/custom_reporters/reporters/TestReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

/**
* TestReporter
* Reporter for testing the outputs, without any extra
* Reporter for testing the outputs, without any extra
* hassle. Uses a JSON like syntax for testing the reporters
* instead of outputting the text to stdout and using match functions
* instead of outputting the text to stdout and using match functions
* to get the output.
*/
class TestReporter {
constructor(options) {
constructor(globalConfig, options) {
this._options = options;

/**
* statsCollected property
* contains most of the statistics
* contains most of the statistics
* related to the object to be called,
* This here helps us in avoiding the string match
* statements nothing else
Expand All @@ -45,35 +45,32 @@ class TestReporter {
}
}

onTestStart(config, path) {
onTestStart(path) {
const onTestStart = this._statsCollected.onTestStart;

onTestStart.called = true;
onTestStart.config = config === undefined;
onTestStart.path = typeof path === 'string';
}

onTestResult(config, testResult, results) {
onTestResult(test, testResult, results) {
const onTestResult = this._statsCollected.onTestResult;

onTestResult.called = true;
onTestResult.times++;
}

onRunStart(config, results, options) {
onRunStart(results, options) {
this.clearLine();
const onRunStart = this._statsCollected.onRunStart;

onRunStart.called = true;
onRunStart.config = typeof config;
onRunStart.options = typeof options;
}

onRunComplete(config, results) {
onRunComplete(contexts, results) {
const onRunComplete = this._statsCollected.onRunComplete;

onRunComplete.called = true;
onRunComplete.config = typeof config;

onRunComplete.numPassedTests = results.numPassedTests;
onRunComplete.numFailedTests = results.numFailedTests;
Expand Down
97 changes: 32 additions & 65 deletions packages/jest-cli/src/ReporterDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,100 +10,67 @@

'use strict';

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

export type RunOptions = {
export type RunOptions = {|
estimatedTime: number,
showStatus: boolean,
};
|};

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

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

register(reporter: Object): void {
register(reporter: Reporter): 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(results: AggregatedResult, options: ReporterOnStartOptions) {
this._reporters.forEach(
reporter => reporter.onRunStart && reporter.onRunStart(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);
}
});
async onRunComplete(contexts: Set<Context>, results: AggregatedResult) {
this._reporters.forEach(
reporter =>
reporter.onRunComplete && reporter.onRunComplete(contexts, 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
Loading

0 comments on commit 8dec8ba

Please sign in to comment.