Skip to content

Commit 7f4e2d4

Browse files
aaronabramovcpojer
authored andcommitted
Custom reporters (jestjs#3349)
* 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
1 parent e97395d commit 7f4e2d4

24 files changed

+791
-164
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Custom Reporters Integration IncompleteReporter for flexibility 1`] = `
4+
"onRunComplete is called
5+
Passed Tests: 1
6+
Failed Tests: 0
7+
Total Tests: 1
8+
"
9+
`;
10+
11+
exports[`Custom Reporters Integration TestReporter with all tests failing 1`] = `
12+
Object {
13+
"onRunComplete": Object {
14+
"called": true,
15+
"numFailedTests": 1,
16+
"numPassedTests": 0,
17+
"numTotalTests": 1,
18+
},
19+
"onRunStart": Object {
20+
"called": true,
21+
"options": "object",
22+
},
23+
"onTestResult": Object {
24+
"called": true,
25+
"times": 1,
26+
},
27+
"onTestStart": Object {
28+
"called": true,
29+
"path": false,
30+
},
31+
"options": Object {
32+
"christoph": "pojer",
33+
"dmitrii": "abramov",
34+
"hello": "world",
35+
},
36+
}
37+
`;
38+
39+
exports[`Custom Reporters Integration TestReporter with all tests passing 1`] = `
40+
Object {
41+
"onRunComplete": Object {
42+
"called": true,
43+
"numFailedTests": 0,
44+
"numPassedTests": 1,
45+
"numTotalTests": 1,
46+
},
47+
"onRunStart": Object {
48+
"called": true,
49+
"options": "object",
50+
},
51+
"onTestResult": Object {
52+
"called": true,
53+
"times": 1,
54+
},
55+
"onTestStart": Object {
56+
"called": true,
57+
"path": false,
58+
},
59+
"options": Object {
60+
"christoph": "pojer",
61+
"dmitrii": "abramov",
62+
"hello": "world",
63+
},
64+
}
65+
`;
66+
67+
exports[`Custom Reporters Integration invalid format for adding reporters 1`] = `
68+
"● Reporter Validation Error:
69+
70+
Unexpected value for Path at index 0 of reporter at index 0
71+
Expected:
72+
string
73+
Got:
74+
number
75+
Reporter configuration:
76+
[
77+
3243242
78+
]
79+
80+
Configuration Documentation:
81+
https://facebook.github.io/jest/docs/configuration.html
82+
83+
"
84+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
'use strict';
9+
10+
const runJest = require('../runJest');
11+
const skipOnWindows = require('skipOnWindows');
12+
13+
describe('Custom Reporters Integration', () => {
14+
skipOnWindows.suite();
15+
16+
test('valid string format for adding reporters', () => {
17+
const reporterConfig = {
18+
reporters: ['<rootDir>/reporters/TestReporter.js'],
19+
};
20+
21+
const {status} = runJest('custom_reporters', [
22+
'--config',
23+
JSON.stringify(reporterConfig),
24+
'add-test.js',
25+
]);
26+
27+
expect(status).toBe(0);
28+
});
29+
30+
test('valid array format for adding reporters', () => {
31+
const reporterConfig = {
32+
reporters: [
33+
['<rootDir>/reporters/TestReporter.js', {'Dmitrii Abramov': 'Awesome'}],
34+
],
35+
};
36+
37+
const {status} = runJest('custom_reporters', [
38+
'--config',
39+
JSON.stringify(reporterConfig),
40+
'add-test.js',
41+
]);
42+
43+
expect(status).toBe(0);
44+
});
45+
46+
test('invalid format for adding reporters', () => {
47+
const reporterConfig = {
48+
reporters: [[3243242]],
49+
};
50+
51+
const {status, stderr} = runJest('custom_reporters', [
52+
'--config',
53+
JSON.stringify(reporterConfig),
54+
'add-test.js',
55+
]);
56+
57+
expect(status).toBe(1);
58+
expect(stderr).toMatchSnapshot();
59+
});
60+
61+
test('TestReporter with all tests passing', () => {
62+
const {stdout, status, stderr} = runJest('custom_reporters', [
63+
'add-test.js',
64+
]);
65+
66+
const parsedJSON = JSON.parse(stdout);
67+
68+
expect(status).toBe(0);
69+
expect(stderr.trim()).toBe('');
70+
expect(parsedJSON).toMatchSnapshot();
71+
});
72+
73+
test('TestReporter with all tests failing', () => {
74+
const {stdout, status, stderr} = runJest('custom_reporters', [
75+
'add-fail-test.js',
76+
]);
77+
78+
const parsedJSON = JSON.parse(stdout);
79+
80+
expect(status).toBe(1);
81+
expect(stderr.trim()).toBe('');
82+
expect(parsedJSON).toMatchSnapshot();
83+
});
84+
85+
test('IncompleteReporter for flexibility', () => {
86+
const {stderr, stdout, status} = runJest('custom_reporters', [
87+
'--no-cache',
88+
'--config',
89+
JSON.stringify({
90+
reporters: ['<rootDir>/reporters/IncompleteReporter.js'],
91+
}),
92+
'add-test.js',
93+
]);
94+
95+
expect(status).toBe(0);
96+
expect(stderr.trim()).toBe('');
97+
98+
expect(stdout).toMatchSnapshot();
99+
});
100+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
*/
9+
10+
'use strict';
11+
12+
const add = require('../add');
13+
14+
describe('CustomReporters', () => {
15+
test('adds fail', () => {
16+
expect(add(1, 3)).toBe(231);
17+
expect(add(5, 7)).toBe(120);
18+
expect(add(2, 4)).toBe(6);
19+
});
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
*/
9+
10+
'use strict';
11+
12+
const add = require('../add');
13+
14+
describe('Custom Reporters', () => {
15+
test('adds ok', () => {
16+
expect(add(1, 2)).toBe(3);
17+
expect(add(3, 4)).toBe(7);
18+
expect(add(12, 24)).toBe(36);
19+
});
20+
});
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
*/
9+
10+
'use strict';
11+
12+
module.exports = (x, y) => x + y;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"jest": {
3+
"reporters": [
4+
["<rootDir>/reporters/TestReporter.js", {
5+
"hello": "world",
6+
"dmitrii": "abramov",
7+
"christoph": "pojer"
8+
}]
9+
]
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
9+
'use strict';
10+
11+
/**
12+
* IncompleteReporter
13+
* Reporter to test for the flexibility of the interface we implemented.
14+
* The reporters shouldn't be required to implement all the methods
15+
*
16+
* This only implements one method onRunComplete which should be called
17+
*/
18+
class IncompleteReporter {
19+
onRunComplete(contexts, results) {
20+
console.log('onRunComplete is called');
21+
console.log('Passed Tests: ' + results.numPassedTests);
22+
console.log('Failed Tests: ' + results.numFailedTests);
23+
console.log('Total Tests: ' + results.numTotalTests);
24+
}
25+
}
26+
27+
module.exports = IncompleteReporter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
9+
'use strict';
10+
11+
/**
12+
* TestReporter
13+
* Reporter for testing the outputs, without any extra
14+
* hassle. Uses a JSON like syntax for testing the reporters
15+
* instead of outputting the text to stdout and using match functions
16+
* to get the output.
17+
*/
18+
class TestReporter {
19+
constructor(globalConfig, options) {
20+
this._options = options;
21+
22+
/**
23+
* statsCollected property
24+
* contains most of the statistics
25+
* related to the object to be called,
26+
* This here helps us in avoiding the string match
27+
* statements nothing else
28+
*/
29+
this._statsCollected = {
30+
onRunComplete: {},
31+
onRunStart: {},
32+
onTestResult: {times: 0},
33+
onTestStart: {},
34+
options,
35+
};
36+
}
37+
38+
/**
39+
* clearLine
40+
* clears the line for easier JSON parsing
41+
*/
42+
clearLine() {
43+
if (process.stdout.isTTY) {
44+
process.stderr.write('\x1b[999D\x1b[K');
45+
}
46+
}
47+
48+
onTestStart(path) {
49+
const onTestStart = this._statsCollected.onTestStart;
50+
51+
onTestStart.called = true;
52+
onTestStart.path = typeof path === 'string';
53+
}
54+
55+
onTestResult(test, testResult, results) {
56+
const onTestResult = this._statsCollected.onTestResult;
57+
58+
onTestResult.called = true;
59+
onTestResult.times++;
60+
}
61+
62+
onRunStart(results, options) {
63+
this.clearLine();
64+
const onRunStart = this._statsCollected.onRunStart;
65+
66+
onRunStart.called = true;
67+
onRunStart.options = typeof options;
68+
}
69+
70+
onRunComplete(contexts, results) {
71+
const onRunComplete = this._statsCollected.onRunComplete;
72+
73+
onRunComplete.called = true;
74+
75+
onRunComplete.numPassedTests = results.numPassedTests;
76+
onRunComplete.numFailedTests = results.numFailedTests;
77+
onRunComplete.numTotalTests = results.numTotalTests;
78+
79+
// The Final Call
80+
process.stdout.write(JSON.stringify(this._statsCollected, null, 4));
81+
}
82+
}
83+
84+
module.exports = TestReporter;

0 commit comments

Comments
 (0)