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

Custom reporters #3349

Merged
merged 4 commits into from
May 2, 2017
Merged

Custom reporters #3349

merged 4 commits into from
May 2, 2017

Conversation

aaronabramov
Copy link
Contributor

i rebased/resolved/squashed #3064 which grew uncontrollably big.
also fixed a few issues and resolved conflicts with multi runner feature

cc @abdulhannanali

@codecov-io
Copy link

codecov-io commented Apr 22, 2017

Codecov Report

Merging #3349 into master will decrease coverage by 0.21%.
The diff coverage is 51.4%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3349      +/-   ##
==========================================
- Coverage   64.02%   63.81%   -0.22%     
==========================================
  Files         177      179       +2     
  Lines        6580     6649      +69     
  Branches        5        5              
==========================================
+ Hits         4213     4243      +30     
- Misses       2365     2404      +39     
  Partials        2        2
Impacted Files Coverage Δ
packages/jest-cli/src/reporters/NotifyReporter.js 25.92% <ø> (ø) ⬆️
packages/jest-config/src/index.js 26.08% <ø> (ø) ⬆️
packages/jest-cli/src/reporters/BaseReporter.js 66.66% <ø> (ø) ⬆️
packages/jest-config/src/validConfig.js 100% <ø> (ø) ⬆️
packages/jest-cli/src/reporters/DefaultReporter.js 16.12% <0%> (ø) ⬆️
packages/jest-cli/src/reporters/VerboseReporter.js 30.61% <0%> (ø) ⬆️
packages/jest-config/src/constants.js 100% <100%> (ø) ⬆️
packages/jest-config/src/normalize.js 86.93% <100%> (-3.01%) ⬇️
...ckages/jest-config/src/reporterValidationErrors.js 33.33% <33.33%> (ø)
packages/jest-cli/src/TestRunner.js 30.85% <35.71%> (-0.29%) ⬇️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 62d29f2...f2eb376. Read the comment docs.

*/
function createReporterError(
reporterIndex: number,
reporterValue: any,
Copy link
Member

Choose a reason for hiding this comment

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

any? Shouldn't it demand at least "Object"?

function createArrayReporterError(
reporterIndex: number,
valueIndex: number,
value: any,
Copy link
Member

Choose a reason for hiding this comment

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

mixed?

@@ -32,6 +32,7 @@ const DEPRECATED_CONFIG = require('./deprecated');
const JSON_EXTENSION = '.json';
const PRESET_NAME = 'jest-preset' + JSON_EXTENSION;
const ERROR = `${BULLET}Validation Error`;
const {validateReporters} = require('./reporterValidationErrors');
Copy link
Member

Choose a reason for hiding this comment

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

this require should be in the block above.


constructor(options: Options) {
constructor(options: GlobalConfig) {
Copy link
Member

Choose a reason for hiding this comment

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

I deliberately changed this in the past to not include all config options. But I guess it is fine – can we change this variable name to _globalConfig and globalConfig?

"path": false,
},
"options": Object {
"christop": "pojer",
Copy link
Member

Choose a reason for hiding this comment

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

typo? :P

exports[`Custom Reporters Integration invalid format for adding reporters 1`] = `
"● Reporter Validation Error:

Unexpected value for Path at index 0 of reporter at index 0
Copy link
Member

Choose a reason for hiding this comment

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

This message is so not nice. index 0 of reporter at index 0. Can we maybe print actual reporters value along with it?

const {stdout, status, stderr} = runJest('custom_reporters', [
'add-test.js',
]);
const parsedJSON = JSON.parse(stdout);
Copy link
Member

Choose a reason for hiding this comment

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

what about runJest.json?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but we're specifically testing a custom reporter :)

Copy link
Member

Choose a reason for hiding this comment

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

Ok, sure then :)


'use strict';

module.exports = function add(x, y) {
Copy link
Member

Choose a reason for hiding this comment

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

have you heard of arrow functions?

* 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
Copy link
Member

Choose a reason for hiding this comment

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

"method"?

const onRunStart = this._statsCollected.onRunStart;

onRunStart.called = true;
onRunStart.config = typeof config;
Copy link
Member

Choose a reason for hiding this comment

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

is this typeof call useful?

import type BaseReporter from './reporters/BaseReporter';
import type {ReporterOnStartOptions} from 'types/Reporters';

export type RunOptions = {
Copy link
Member

Choose a reason for hiding this comment

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

should this be an exact type?

@@ -67,7 +69,7 @@ class TestRunner {
this._setupReporters();
}

addReporter(reporter: BaseReporter) {
addReporter(reporter: Object) {
Copy link
Member

Choose a reason for hiding this comment

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

Can this be a type for Reporter instances in types/TestRunner.js or something?

@@ -273,14 +275,22 @@ class TestRunner {
return Promise.race([runAllTests, onInterrupt]).then(cleanup, cleanup);
}

_shouldAddDefaultReporters(reporters?: Array<ReporterConfig>): boolean {
return !reporters || reporters.indexOf(DEFAULT_REPORTER_LABEL) !== -1;
Copy link
Member

Choose a reason for hiding this comment

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

What happens when reporters.length is 0?

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'd expect it to give no output, i think

Copy link
Member

Choose a reason for hiding this comment

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

I think we should throw in that case, tbh.

_setupReporters() {
const config = this._config;
const reporters = config.reporters;
const isDefault: boolean = this._shouldAddDefaultReporters(reporters);
Copy link
Member

Choose a reason for hiding this comment

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

does this really need a type annotation inline? Flow should be able to figure it out.

@cpojer
Copy link
Member

cpojer commented Apr 25, 2017

Bunch of comments but feel free to merge it after one more iteration.

@cpojer cpojer added this to the Jest 20 milestone Apr 28, 2017
@cpojer
Copy link
Member

cpojer commented May 2, 2017

Please don't forget to document the reporters option in the docs and adding "Available in Jest 20.0.0" like we do for other things (Like the expect.resolves API).

Copy link
Member

@cpojer cpojer left a comment

Choose a reason for hiding this comment

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

I'll request changes because you are doing some bigger rewrites and I'll take a look at it tomorrow :)

this error is not letting me lint 100% safe

Reporter config 3 (#2)

* add custom reporters option in TestRunner

* add reporters option in jest-cli config

* add flowtype for reporters option

* add key for reporters in validConfig

* add noDefaultReporters option

noDefaultReporters option let's user turn
off all the reporters set by default

* Lint

* add unit tests for _addCustomReporters

* separate default reporters in method in TestRunner

* add tests for errors which are thrown

* add tests for .noDefaultReporters

* modify Error thrown for _addCustomReporters

* remove superfluous comment from TestRunner.js

* remove reporter tests from TestRunner-test.js

* add new custom reporters format in TestRunner.js

* update the format for adding customReporter

* add descriptive validations for reporters

* add reporters attibute in normalize.js

* add prettier to types

* Seperate out ReporterDispatcher in a file

* add elaborate messages for errors

* add Facebook Copyright header to ReporterDispatcher.js

* typecheck and lint properly

* correcting a condition in ReporterDispatcher

* rename method to `_shouldAddDefaultReporters`

* add integration tests for custom_reporters

* add more complete integration tests for reporters

* remove AggregatedResults.js

* remove any methods to be validated

* correct _addDefaultReporters call

* remove "reporters" validations from TestRunner.js

* add pretty validations for custom reporters

* remove comment

* add reporter validation in normalize.js

* keep comments precise remove unwanted

* check if reporters exist before validation

* pretty custom reporters

* prettier integration_tests

* prettier

* yarn prettier

* prettier

* Remove unnecessary comments from TestRunner.js

* make ReporterConfig type in types/Config simpler

* remove comments

* correct types and change method signatures

* remove bug from reporterValidationErrors.js

* make custom_reporters tests more concise

* fix lint error in website

this error is not letting me lint 100% safe

* finalize types for reporters

* yarn prettier

* remove .vscode folder

* all integration_tests are prettier now

* remove validateReporters call

* remove usage of \t in reporter validation errors

* change spread operator with usage of .apply

* modify custom_reporters integration_tests to suit node 4

* prettier validations

* prettier ❤️

* pretty lint

* update lock file
* This only implements one method onRunComplete which should be called
*/
class IncompleteReporter {
constructor(options) {
Copy link
Member

Choose a reason for hiding this comment

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

this seems unused.

Got:
number
Reporters config:
[ 3243242 ]
Copy link
Member

Choose a reason for hiding this comment

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

This whitespace is really ugly. Why can we not just print this using prettyFormat like we always do?

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 i missed join('\n') somewhere :)

this.addReporter(
config.verbose
? new VerboseReporter(config)
: new DefaultReporter(this._globalConfig),
Copy link
Member

Choose a reason for hiding this comment

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

this is passing config once and this._globalConfig on the other, even though they are the same.

}

_addCustomReporters(reporters: Array<ReporterConfig>) {
const customReporter = reporters.filter(reporter => reporter !== 'default');
Copy link
Member

Choose a reason for hiding this comment

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

this should be customReporters, plural.

chalk.red(
'An error occured while adding the reporter at path ' + path,
),
);
Copy link
Member

Choose a reason for hiding this comment

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

why does this write to console.error and then throw? Shouldn't it be:

throw new Error(
  'An error occured while adding the reporter at path "' + path + '".' +
    error.message
);

* Get properties of a reporter in an object
* to make dealing with them less painful.
*/
_getReporterProps(reporter: ReporterConfig): Object {
Copy link
Member

Choose a reason for hiding this comment

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

I think you can remote Object here because it is a private field. If not, then please type it properly :)

* to make dealing with them less painful.
*/
_getReporterProps(reporter: ReporterConfig): Object {
let props = {};
Copy link
Member

Choose a reason for hiding this comment

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

Object.create(null).

import type {PathPattern} from '../SearchSource';
import type {ReporterOnStartOptions} from 'types/Reporters';

type SummaryReporterOptions = {
Copy link
Member

Choose a reason for hiding this comment

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

exact type please.

@@ -4,8 +4,6 @@
* 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.
*
* @emails oncall+jsinfra
Copy link
Member

Choose a reason for hiding this comment

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

we should remove this from all tests in this repo, actually.

@@ -255,6 +256,10 @@ function normalize(options: InitialOptions, argv: Object = {}) {
exampleConfig: VALID_CONFIG,
});

if (options.reporters && Array.isArray(options.reporters)) {
Copy link
Member

Choose a reason for hiding this comment

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

If it is not an array, shouldn't we also validate it?

@cpojer
Copy link
Member

cpojer commented May 2, 2017

I left a few comments inline. The one missing piece is that the <rootDir> replacement and proper resolution using jest-resolve (we do this in normalize.js for all other fields) must be added, otherwise resolving reporters works differently from every other config option. Mind adding that?

@cpojer cpojer merged commit 4a99428 into jestjs:master May 2, 2017
@cpojer
Copy link
Member

cpojer commented May 2, 2017

Alright, good to go.

@aaronabramov aaronabramov deleted the reporter-config-4 branch May 2, 2017 23:47
@SimenB SimenB mentioned this pull request Jun 27, 2017
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Custom Reporters

this error is not letting me lint 100% safe

Reporter config 3 (jestjs#2)

* add custom reporters option in TestRunner

* add reporters option in jest-cli config

* add flowtype for reporters option

* add key for reporters in validConfig

* add noDefaultReporters option

noDefaultReporters option let's user turn
off all the reporters set by default

* Lint

* add unit tests for _addCustomReporters

* separate default reporters in method in TestRunner

* add tests for errors which are thrown

* add tests for .noDefaultReporters

* modify Error thrown for _addCustomReporters

* remove superfluous comment from TestRunner.js

* remove reporter tests from TestRunner-test.js

* add new custom reporters format in TestRunner.js

* update the format for adding customReporter

* add descriptive validations for reporters

* add reporters attibute in normalize.js

* add prettier to types

* Seperate out ReporterDispatcher in a file

* add elaborate messages for errors

* add Facebook Copyright header to ReporterDispatcher.js

* typecheck and lint properly

* correcting a condition in ReporterDispatcher

* rename method to `_shouldAddDefaultReporters`

* add integration tests for custom_reporters

* add more complete integration tests for reporters

* remove AggregatedResults.js

* remove any methods to be validated

* correct _addDefaultReporters call

* remove "reporters" validations from TestRunner.js

* add pretty validations for custom reporters

* remove comment

* add reporter validation in normalize.js

* keep comments precise remove unwanted

* check if reporters exist before validation

* pretty custom reporters

* prettier integration_tests

* prettier

* yarn prettier

* prettier

* Remove unnecessary comments from TestRunner.js

* make ReporterConfig type in types/Config simpler

* remove comments

* correct types and change method signatures

* remove bug from reporterValidationErrors.js

* make custom_reporters tests more concise

* fix lint error in website

this error is not letting me lint 100% safe

* finalize types for reporters

* yarn prettier

* remove .vscode folder

* all integration_tests are prettier now

* remove validateReporters call

* remove usage of \t in reporter validation errors

* change spread operator with usage of .apply

* modify custom_reporters integration_tests to suit node 4

* prettier validations

* prettier ❤️

* pretty lint

* update lock file

* Custom Reporters (merge/fix)

* Use jest-resolve to resolve reporters

* Minor cleanups
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants