-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
86 lines (77 loc) · 2.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Summary reporter
Intended to be added to the default reporter.
See README.md for installation.
*/
const {relative} = require('path');
const chalk = require('chalk');
const MARKER_FOR_STATUS = {
passed: chalk.green('✓'),
failed: chalk.red('✕'),
pending: chalk.yellow('○')
}
const fileStyle = chalk.bold.white;
const titleStyle = chalk.grey;
const pathStyle = chalk.white;
const summaryLineStyle = chalk.bold.cyan.underline;
const INDENT = ' ';
class SummaryReporter {
/**
* @param {*} globalConfig
* @param {*} options
*/
constructor({rootDir}, {failuresOnly=true}={}) {
this._rootDir = rootDir;
this._failuresOnly = failuresOnly;
this._indent = INDENT;
this._path = [];
}
log(message) {
process.stderr.write(message + '\n');
}
onRunComplete(contexts, results) {
this.log('');
this.log(summaryLineStyle(this.summaryLine(results)));
this.log('');
for (let file of this.sortResults(results.testResults)) {
if (this._failuresOnly && !file.numFailingTests) continue;
this.resetPath();
this.log(fileStyle(relative(this._rootDir, file.testFilePath)));
for (let {status, ancestorTitles, title} of file.testResults) {
if (this._failuresOnly && status !== 'failed') continue;
this.printPath(ancestorTitles);
this.log(`${this._indent}${MARKER_FOR_STATUS[status]||status} ${titleStyle(title)}`);
}
}
this.log('');
}
/** Return copy of input, sorting by filename alphabetically.
* Sorting ensures the output is stable, needed for snapshot testing.
*/
sortResults(testResults) {
let prop = 'testFilePath';
return testResults.concat().sort((a, b) => a.testFilePath.localeCompare(b.testFilePath));
}
summaryLine({numFailedTestSuites, numPassedTests}) {
if (numFailedTestSuites == 0) {
return numPassedTests ? 'All tests passed' : 'All tests skipped';
}
return this._failuresOnly ? 'Summary of failed tests' : 'Summary of tests';
}
printPath(ancestorTitles) {
let p = this._path, a = ancestorTitles;
while (p.length > 0 && a.length > 0 && p[p.length-1] != a[a.length-1]) {
p.pop();
}
this._indent = INDENT.repeat(p.length+1);
for (let i = p.length; i < a.length; i++) {
this.log(this._indent + pathStyle(a[i]));
p.push(a[i]);
this._indent += INDENT;
}
}
resetPath() {
this._path = [];
this._indent = INDENT;
}
}
module.exports = SummaryReporter;