UNPKG

4.9 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8function _chalk() {
9 const data = _interopRequireDefault(require('chalk'));
10
11 _chalk = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _jestUtil() {
19 const data = require('jest-util');
20
21 _jestUtil = function () {
22 return data;
23 };
24
25 return data;
26}
27
28var _DefaultReporter = _interopRequireDefault(require('./DefaultReporter'));
29
30function _interopRequireDefault(obj) {
31 return obj && obj.__esModule ? obj : {default: obj};
32}
33
34function _defineProperty(obj, key, value) {
35 if (key in obj) {
36 Object.defineProperty(obj, key, {
37 value: value,
38 enumerable: true,
39 configurable: true,
40 writable: true
41 });
42 } else {
43 obj[key] = value;
44 }
45 return obj;
46}
47
48const {ICONS} = _jestUtil().specialChars;
49
50class VerboseReporter extends _DefaultReporter.default {
51 constructor(globalConfig) {
52 super(globalConfig);
53
54 _defineProperty(this, '_globalConfig', void 0);
55
56 this._globalConfig = globalConfig;
57 }
58
59 static filterTestResults(testResults) {
60 return testResults.filter(({status}) => status !== 'pending');
61 }
62
63 static groupTestsBySuites(testResults) {
64 const root = {
65 suites: [],
66 tests: [],
67 title: ''
68 };
69 testResults.forEach(testResult => {
70 let targetSuite = root; // Find the target suite for this test,
71 // creating nested suites as necessary.
72
73 for (const title of testResult.ancestorTitles) {
74 let matchingSuite = targetSuite.suites.find(s => s.title === title);
75
76 if (!matchingSuite) {
77 matchingSuite = {
78 suites: [],
79 tests: [],
80 title
81 };
82 targetSuite.suites.push(matchingSuite);
83 }
84
85 targetSuite = matchingSuite;
86 }
87
88 targetSuite.tests.push(testResult);
89 });
90 return root;
91 }
92
93 onTestResult(test, result, aggregatedResults) {
94 super.testFinished(test.context.config, result, aggregatedResults);
95
96 if (!result.skipped) {
97 this.printTestFileHeader(
98 result.testFilePath,
99 test.context.config,
100 result
101 );
102
103 if (!result.testExecError && !result.skipped) {
104 this._logTestResults(result.testResults);
105 }
106
107 this.printTestFileFailureMessage(
108 result.testFilePath,
109 test.context.config,
110 result
111 );
112 }
113
114 super.forceFlushBufferedOutput();
115 }
116
117 _logTestResults(testResults) {
118 this._logSuite(VerboseReporter.groupTestsBySuites(testResults), 0);
119
120 this._logLine();
121 }
122
123 _logSuite(suite, indentLevel) {
124 if (suite.title) {
125 this._logLine(suite.title, indentLevel);
126 }
127
128 this._logTests(suite.tests, indentLevel + 1);
129
130 suite.suites.forEach(suite => this._logSuite(suite, indentLevel + 1));
131 }
132
133 _getIcon(status) {
134 if (status === 'failed') {
135 return _chalk().default.red(ICONS.failed);
136 } else if (status === 'pending') {
137 return _chalk().default.yellow(ICONS.pending);
138 } else if (status === 'todo') {
139 return _chalk().default.magenta(ICONS.todo);
140 } else {
141 return _chalk().default.green(ICONS.success);
142 }
143 }
144
145 _logTest(test, indentLevel) {
146 const status = this._getIcon(test.status);
147
148 const time = test.duration
149 ? ` (${(0, _jestUtil().formatTime)(Math.round(test.duration))})`
150 : '';
151
152 this._logLine(
153 status + ' ' + _chalk().default.dim(test.title + time),
154 indentLevel
155 );
156 }
157
158 _logTests(tests, indentLevel) {
159 if (this._globalConfig.expand) {
160 tests.forEach(test => this._logTest(test, indentLevel));
161 } else {
162 const summedTests = tests.reduce(
163 (result, test) => {
164 if (test.status === 'pending') {
165 result.pending.push(test);
166 } else if (test.status === 'todo') {
167 result.todo.push(test);
168 } else {
169 this._logTest(test, indentLevel);
170 }
171
172 return result;
173 },
174 {
175 pending: [],
176 todo: []
177 }
178 );
179
180 if (summedTests.pending.length > 0) {
181 summedTests.pending.forEach(this._logTodoOrPendingTest(indentLevel));
182 }
183
184 if (summedTests.todo.length > 0) {
185 summedTests.todo.forEach(this._logTodoOrPendingTest(indentLevel));
186 }
187 }
188 }
189
190 _logTodoOrPendingTest(indentLevel) {
191 return test => {
192 const printedTestStatus =
193 test.status === 'pending' ? 'skipped' : test.status;
194
195 const icon = this._getIcon(test.status);
196
197 const text = _chalk().default.dim(`${printedTestStatus} ${test.title}`);
198
199 this._logLine(`${icon} ${text}`, indentLevel);
200 };
201 }
202
203 _logLine(str, indentLevel) {
204 const indentation = ' '.repeat(indentLevel || 0);
205 this.log(indentation + (str || ''));
206 }
207}
208
209exports.default = VerboseReporter;
210
211_defineProperty(VerboseReporter, 'filename', __filename);