-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
94 lines (80 loc) · 2.29 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
87
88
89
90
91
92
93
94
const {
green,
red,
gray,
cyan,
white,
} = require('chalk');
const {
duration,
formatTitle,
} = require('./lib/utils');
const { log } = console;
class JestCustomReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig;
this._options = options;
}
onRunStart({ numTotalTestSuites }) {
log();
log(gray(`Found ${numTotalTestSuites} test suites`));
}
/**
*
* @param {String} prevTitle
* @param {Array} testResults
* @param {Number} resultsIndex
* @param {Number} titlesIndex
*/
recursivelyReport(prevTitle, testResults, resultsIndex, titlesIndex) {
const testResult = testResults[resultsIndex];
if (!testResult) {
// exit at end of testResults array
return;
}
const { ancestorTitles, status, title } = testResult;
const currentTitle = ancestorTitles[titlesIndex];
if (!currentTitle) {
// if past the end of ancestorTitles, go back one index
this.recursivelyReport(prevTitle, testResults, resultsIndex, --titlesIndex);
return;
}
if (prevTitle !== currentTitle && titlesIndex < ancestorTitles.length) {
// if new title encountered and not yet at the end of ancestorTitles, check next ancestorTitle
log(white(currentTitle));
this.recursivelyReport(currentTitle, testResults, resultsIndex, ++titlesIndex);
} else {
// otherwise log actual test and go onto next test
log(formatTitle(status, title));
this.recursivelyReport(currentTitle, testResults, ++resultsIndex, titlesIndex);
}
}
onRunComplete(test, results) {
const {
numFailedTests,
numPassedTests,
numPendingTests,
testResults,
startTime,
} = results;
testResults.forEach(({ testFilePath, testResults, failureMessage }) => {
this.recursivelyReport(testFilePath, testResults, 0, 0);
if (failureMessage) {
log(failureMessage);
}
log();
});
if (numPassedTests) {
const end = new Date();
const start = new Date(startTime);
log(green(`${numPassedTests} passing`), gray(`(${duration(end, start)})`));
}
if (numFailedTests) {
log(red(`${numFailedTests} failing`));
}
if (numPendingTests) {
log(cyan(`${numPendingTests} pending`));
}
}
}
module.exports = JestCustomReporter;