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 reporter integration #3064

Closed
wants to merge 63 commits into from
Closed
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
98d01b5
add custom reporters option in TestRunner
abdulhannanali Mar 3, 2017
1215c0b
add reporters option in jest-cli config
abdulhannanali Mar 4, 2017
b3196ac
add flowtype for reporters option
abdulhannanali Mar 4, 2017
44f6550
add key for reporters in validConfig
abdulhannanali Mar 4, 2017
aa548c9
add noDefaultReporters option
abdulhannanali Mar 5, 2017
268a640
Lint
abdulhannanali Mar 5, 2017
e79ae0c
add unit tests for _addCustomReporters
abdulhannanali Mar 5, 2017
2ee53ce
separate default reporters in method in TestRunner
abdulhannanali Mar 6, 2017
073d6e9
add tests for errors which are thrown
abdulhannanali Mar 6, 2017
6a430de
add tests for .noDefaultReporters
abdulhannanali Mar 6, 2017
329df49
Merge branch 'master' into reporter-config
abdulhannanali Mar 6, 2017
7cc7aea
modify Error thrown for _addCustomReporters
abdulhannanali Mar 8, 2017
138bec3
remove superfluous comment from TestRunner.js
abdulhannanali Mar 8, 2017
6da8dad
remove reporter tests from TestRunner-test.js
abdulhannanali Apr 1, 2017
352380b
add new custom reporters format in TestRunner.js
abdulhannanali Apr 1, 2017
1a5ac39
update the format for adding customReporter
abdulhannanali Apr 1, 2017
124ee97
Merge branch 'master' into reporter-config
abdulhannanali Apr 1, 2017
d067e59
add descriptive validations for reporters
abdulhannanali Apr 1, 2017
302b672
add reporters attibute in normalize.js
abdulhannanali Apr 1, 2017
a9e9fce
add prettier to types
abdulhannanali Apr 1, 2017
31b51e3
Seperate out ReporterDispatcher in a file
abdulhannanali Apr 1, 2017
75e39d1
add elaborate messages for errors
abdulhannanali Apr 1, 2017
12e5382
add Facebook Copyright header to ReporterDispatcher.js
abdulhannanali Apr 1, 2017
08bd0cf
typecheck and lint properly
abdulhannanali Apr 1, 2017
8bf1bb8
correcting a condition in ReporterDispatcher
abdulhannanali Apr 2, 2017
06e7103
rename method to `_shouldAddDefaultReporters`
abdulhannanali Apr 2, 2017
32dcff4
add integration tests for custom_reporters
abdulhannanali Apr 2, 2017
45656ad
add more complete integration tests for reporters
abdulhannanali Apr 2, 2017
15abe4e
remove AggregatedResults.js
abdulhannanali Apr 2, 2017
76ebadb
remove any methods to be validated
abdulhannanali Apr 2, 2017
e158496
correct _addDefaultReporters call
abdulhannanali Apr 2, 2017
80c88db
remove "reporters" validations from TestRunner.js
abdulhannanali Apr 3, 2017
4a33e50
add pretty validations for custom reporters
abdulhannanali Apr 3, 2017
352f219
remove comment
abdulhannanali Apr 3, 2017
03c2045
add reporter validation in normalize.js
abdulhannanali Apr 3, 2017
8dd5ef3
keep comments precise remove unwanted
abdulhannanali Apr 3, 2017
27c5533
check if reporters exist before validation
abdulhannanali Apr 3, 2017
cebb62e
pretty custom reporters
abdulhannanali Apr 4, 2017
e36d442
prettier integration_tests
abdulhannanali Apr 4, 2017
593040c
prettier
abdulhannanali Apr 4, 2017
c94c993
yarn prettier
abdulhannanali Apr 4, 2017
fc8a4d3
prettier
abdulhannanali Apr 4, 2017
cfdcccf
Remove unnecessary comments from TestRunner.js
abdulhannanali Apr 4, 2017
552d0e2
make ReporterConfig type in types/Config simpler
abdulhannanali Apr 4, 2017
7d903c9
remove comments
abdulhannanali Apr 4, 2017
1aa5d54
correct types and change method signatures
abdulhannanali Apr 4, 2017
599c6ed
remove bug from reporterValidationErrors.js
abdulhannanali Apr 5, 2017
b5aa966
make custom_reporters tests more concise
abdulhannanali Apr 5, 2017
d1cf92e
fix lint error in website
abdulhannanali Apr 5, 2017
23a7610
finalize types for reporters
abdulhannanali Apr 5, 2017
dd2d652
yarn prettier
abdulhannanali Apr 5, 2017
33640be
remove .vscode folder
abdulhannanali Apr 5, 2017
937a3c9
all integration_tests are prettier now
abdulhannanali Apr 5, 2017
6dc65fb
Merge branch 'master' into reporter-config
abdulhannanali Apr 5, 2017
492e0de
remove validateReporters call
abdulhannanali Apr 5, 2017
8c486e7
remove usage of \t in reporter validation errors
abdulhannanali Apr 10, 2017
e328f93
change spread operator with usage of .apply
abdulhannanali Apr 10, 2017
bfad068
modify custom_reporters integration_tests to suit node 4
abdulhannanali Apr 11, 2017
f4ed436
prettier validations
abdulhannanali Apr 11, 2017
3b154d2
Merge remote-tracking branch 'remotes/upstream/master' into reporter-…
abdulhannanali Apr 11, 2017
f6fa7bb
prettier :heart:
abdulhannanali Apr 11, 2017
9ac6de0
pretty lint
abdulhannanali Apr 11, 2017
e093125
update lock file
abdulhannanali Apr 11, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .vscode/settings.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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,
"config": "object",
"numFailedTests": 1,
"numPassedTests": 0,
"numTotalTests": 1,
},
"onRunStart": Object {
"called": true,
"config": "object",
"options": "object",
},
"onTestResult": Object {
"called": true,
"times": 1,
},
"onTestStart": Object {
"called": true,
"config": false,
"path": true,
},
"options": Object {
"christop": "pojer",
"dmitrii": "abramov",
"hello": "world",
},
}
`;

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 {
"called": true,
"times": 1,
},
"onTestStart": Object {
"called": true,
"config": false,
"path": true,
},
"options": Object {
"christop": "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

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.
*/
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();
Copy link
Contributor

Choose a reason for hiding this comment

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

you can probably remove other expectations on JSON since you're matching to a snapshot here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DmitriiAbramov I thought it would be a good idea in order to make like TOTALLY sure, but that's overdone, will remove them

});

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', [
'--config',
JSON.stringify({
reporters: ['<rootDir>/reporters/IncompleteReporter.js'],
}),
'add-test.js',
]);

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

expect(stdout).toMatchSnapshot();
Copy link
Contributor

Choose a reason for hiding this comment

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

also make sure there's no timestapms logged in the stdout, otherwise the snapshot will fail on every new run!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DmitriiAbramov Yes I am making sure, that's one of the reasons TestReporter gives it's own little generated JSON instead of just stringifying aggregatedResults and matching that as a snapshot

});
});
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
9 changes: 9 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,9 @@
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);
});
});
9 changes: 9 additions & 0 deletions integration_tests/custom_reporters/__tests__/add-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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);
});
});
3 changes: 3 additions & 0 deletions integration_tests/custom_reporters/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function add(x, y) {
return 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",
"christop": "pojer"
}]
]
}
}
29 changes: 29 additions & 0 deletions integration_tests/custom_reporters/reporters/IncompleteReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.
*/

/**
* 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) {
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;
85 changes: 85 additions & 0 deletions integration_tests/custom_reporters/reporters/TestReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* 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.
*/

/**
* 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(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(config, path) {
const {onTestStart} = this._statsCollected;

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

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

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

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

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

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

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

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