-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Changes from 55 commits
98d01b5
1215c0b
b3196ac
44f6550
aa548c9
268a640
e79ae0c
2ee53ce
073d6e9
6a430de
329df49
7cc7aea
138bec3
6da8dad
352380b
1a5ac39
124ee97
d067e59
302b672
a9e9fce
31b51e3
75e39d1
12e5382
08bd0cf
8bf1bb8
06e7103
32dcff4
45656ad
15abe4e
76ebadb
e158496
80c88db
4a33e50
352f219
03c2045
8dd5ef3
27c5533
cebb62e
e36d442
593040c
c94c993
fc8a4d3
cfdcccf
552d0e2
7d903c9
1aa5d54
599c6ed
b5aa966
d1cf92e
23a7610
dd2d652
33640be
937a3c9
6dc65fb
492e0de
8c486e7
e328f93
bfad068
f4ed436
3b154d2
f6fa7bb
9ac6de0
e093125
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
" | ||
`; |
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(); | ||
}); | ||
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
@@ -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); | ||
}); | ||
}); |
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); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function add(x, y) { | ||
return x + y; | ||
}; |
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" | ||
}] | ||
] | ||
} | ||
} |
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; |
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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