Skip to content

Commit

Permalink
Custom reporters (#3349)
Browse files Browse the repository at this point in the history
* Custom Reporters

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

* Custom Reporters (merge/fix)

* Use jest-resolve to resolve reporters

* Minor cleanups
  • Loading branch information
aaronabramov authored and cpojer committed May 2, 2017
1 parent 643d9a8 commit 4a99428
Show file tree
Hide file tree
Showing 24 changed files with 791 additions and 164 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Custom Reporters Integration IncompleteReporter for flexibility 1`] = `
"onRunComplete is called
Passed Tests: 1
Failed Tests: 0
Total Tests: 1
"
`;

exports[`Custom Reporters Integration TestReporter with all tests failing 1`] = `
Object {
"onRunComplete": Object {
"called": true,
"numFailedTests": 1,
"numPassedTests": 0,
"numTotalTests": 1,
},
"onRunStart": Object {
"called": true,
"options": "object",
},
"onTestResult": Object {
"called": true,
"times": 1,
},
"onTestStart": Object {
"called": true,
"path": false,
},
"options": Object {
"christoph": "pojer",
"dmitrii": "abramov",
"hello": "world",
},
}
`;

exports[`Custom Reporters Integration TestReporter with all tests passing 1`] = `
Object {
"onRunComplete": Object {
"called": true,
"numFailedTests": 0,
"numPassedTests": 1,
"numTotalTests": 1,
},
"onRunStart": Object {
"called": true,
"options": "object",
},
"onTestResult": Object {
"called": true,
"times": 1,
},
"onTestStart": Object {
"called": true,
"path": false,
},
"options": Object {
"christoph": "pojer",
"dmitrii": "abramov",
"hello": "world",
},
}
`;

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
Expected:
string
Got:
number
Reporter configuration:
[
3243242
]
Configuration Documentation:
https://facebook.github.io/jest/docs/configuration.html
"
`;
100 changes: 100 additions & 0 deletions integration_tests/__tests__/custom-reporters-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* 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 runJest = require('../runJest');
const skipOnWindows = require('skipOnWindows');

describe('Custom Reporters Integration', () => {
skipOnWindows.suite();

test('valid string format for adding reporters', () => {
const reporterConfig = {
reporters: ['<rootDir>/reporters/TestReporter.js'],
};

const {status} = runJest('custom_reporters', [
'--config',
JSON.stringify(reporterConfig),
'add-test.js',
]);

expect(status).toBe(0);
});

test('valid array format for adding reporters', () => {
const reporterConfig = {
reporters: [
['<rootDir>/reporters/TestReporter.js', {'Dmitrii Abramov': 'Awesome'}],
],
};

const {status} = runJest('custom_reporters', [
'--config',
JSON.stringify(reporterConfig),
'add-test.js',
]);

expect(status).toBe(0);
});

test('invalid format for adding reporters', () => {
const reporterConfig = {
reporters: [[3243242]],
};

const {status, stderr} = runJest('custom_reporters', [
'--config',
JSON.stringify(reporterConfig),
'add-test.js',
]);

expect(status).toBe(1);
expect(stderr).toMatchSnapshot();
});

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

const parsedJSON = JSON.parse(stdout);

expect(status).toBe(0);
expect(stderr.trim()).toBe('');
expect(parsedJSON).toMatchSnapshot();
});

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

const parsedJSON = JSON.parse(stdout);

expect(status).toBe(1);
expect(stderr.trim()).toBe('');
expect(parsedJSON).toMatchSnapshot();
});

test('IncompleteReporter for flexibility', () => {
const {stderr, stdout, status} = runJest('custom_reporters', [
'--no-cache',
'--config',
JSON.stringify({
reporters: ['<rootDir>/reporters/IncompleteReporter.js'],
}),
'add-test.js',
]);

expect(status).toBe(0);
expect(stderr.trim()).toBe('');

expect(stdout).toMatchSnapshot();
});
});
20 changes: 20 additions & 0 deletions integration_tests/custom_reporters/__tests__/add-fail-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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', () => {
test('adds fail', () => {
expect(add(1, 3)).toBe(231);
expect(add(5, 7)).toBe(120);
expect(add(2, 4)).toBe(6);
});
});
20 changes: 20 additions & 0 deletions integration_tests/custom_reporters/__tests__/add-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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', () => {
test('adds ok', () => {
expect(add(1, 2)).toBe(3);
expect(add(3, 4)).toBe(7);
expect(add(12, 24)).toBe(36);
});
});
12 changes: 12 additions & 0 deletions integration_tests/custom_reporters/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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;
11 changes: 11 additions & 0 deletions integration_tests/custom_reporters/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"jest": {
"reporters": [
["<rootDir>/reporters/TestReporter.js", {
"hello": "world",
"dmitrii": "abramov",
"christoph": "pojer"
}]
]
}
}
27 changes: 27 additions & 0 deletions integration_tests/custom_reporters/reporters/IncompleteReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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';

/**
* 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 method onRunComplete which should be called
*/
class IncompleteReporter {
onRunComplete(contexts, results) {
console.log('onRunComplete is called');
console.log('Passed Tests: ' + results.numPassedTests);
console.log('Failed Tests: ' + results.numFailedTests);
console.log('Total Tests: ' + results.numTotalTests);
}
}

module.exports = IncompleteReporter;
84 changes: 84 additions & 0 deletions integration_tests/custom_reporters/reporters/TestReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* 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';

/**
* TestReporter
* 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
* to get the output.
*/
class TestReporter {
constructor(globalConfig, options) {
this._options = options;

/**
* statsCollected property
* contains most of the statistics
* related to the object to be called,
* This here helps us in avoiding the string match
* statements nothing else
*/
this._statsCollected = {
onRunComplete: {},
onRunStart: {},
onTestResult: {times: 0},
onTestStart: {},
options,
};
}

/**
* clearLine
* clears the line for easier JSON parsing
*/
clearLine() {
if (process.stdout.isTTY) {
process.stderr.write('\x1b[999D\x1b[K');
}
}

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

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

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

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

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

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

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

onRunComplete.called = true;

onRunComplete.numPassedTests = results.numPassedTests;
onRunComplete.numFailedTests = results.numFailedTests;
onRunComplete.numTotalTests = results.numTotalTests;

// The Final Call
process.stdout.write(JSON.stringify(this._statsCollected, null, 4));
}
}

module.exports = TestReporter;
Loading

0 comments on commit 4a99428

Please sign in to comment.