UNPKG

5.12 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
34/**
35 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
36 *
37 * This source code is licensed under the MIT license found in the
38 * LICENSE file in the root directory of this source tree.
39 */
40const {ICONS} = _jestUtil().specialChars;
41
42class VerboseReporter extends _DefaultReporter.default {
43 _globalConfig;
44 static filename = __filename;
45
46 constructor(globalConfig) {
47 super(globalConfig);
48 this._globalConfig = globalConfig;
49 } // Verbose mode is for debugging. Buffering of output is undesirable.
50 // See https://github.com/facebook/jest/issues/8208
51
52 __wrapStdio(stream) {
53 const write = stream.write.bind(stream);
54
55 stream.write = chunk => {
56 this.__clearStatus();
57
58 write(chunk);
59
60 this.__printStatus();
61
62 return true;
63 };
64 }
65
66 static filterTestResults(testResults) {
67 return testResults.filter(({status}) => status !== 'pending');
68 }
69
70 static groupTestsBySuites(testResults) {
71 const root = {
72 suites: [],
73 tests: [],
74 title: ''
75 };
76 testResults.forEach(testResult => {
77 let targetSuite = root; // Find the target suite for this test,
78 // creating nested suites as necessary.
79
80 for (const title of testResult.ancestorTitles) {
81 let matchingSuite = targetSuite.suites.find(s => s.title === title);
82
83 if (!matchingSuite) {
84 matchingSuite = {
85 suites: [],
86 tests: [],
87 title
88 };
89 targetSuite.suites.push(matchingSuite);
90 }
91
92 targetSuite = matchingSuite;
93 }
94
95 targetSuite.tests.push(testResult);
96 });
97 return root;
98 }
99
100 onTestResult(test, result, aggregatedResults) {
101 super.testFinished(test.context.config, result, aggregatedResults);
102
103 if (!result.skipped) {
104 this.printTestFileHeader(
105 result.testFilePath,
106 test.context.config,
107 result
108 );
109
110 if (!result.testExecError && !result.skipped) {
111 this._logTestResults(result.testResults);
112 }
113
114 this.printTestFileFailureMessage(
115 result.testFilePath,
116 test.context.config,
117 result
118 );
119 }
120
121 super.forceFlushBufferedOutput();
122 }
123
124 _logTestResults(testResults) {
125 this._logSuite(VerboseReporter.groupTestsBySuites(testResults), 0);
126
127 this._logLine();
128 }
129
130 _logSuite(suite, indentLevel) {
131 if (suite.title) {
132 this._logLine(suite.title, indentLevel);
133 }
134
135 this._logTests(suite.tests, indentLevel + 1);
136
137 suite.suites.forEach(suite => this._logSuite(suite, indentLevel + 1));
138 }
139
140 _getIcon(status) {
141 if (status === 'failed') {
142 return _chalk().default.red(ICONS.failed);
143 } else if (status === 'pending') {
144 return _chalk().default.yellow(ICONS.pending);
145 } else if (status === 'todo') {
146 return _chalk().default.magenta(ICONS.todo);
147 } else {
148 return _chalk().default.green(ICONS.success);
149 }
150 }
151
152 _logTest(test, indentLevel) {
153 const status = this._getIcon(test.status);
154
155 const time = test.duration
156 ? ` (${(0, _jestUtil().formatTime)(Math.round(test.duration))})`
157 : '';
158
159 this._logLine(
160 `${status} ${_chalk().default.dim(test.title + time)}`,
161 indentLevel
162 );
163 }
164
165 _logTests(tests, indentLevel) {
166 if (this._globalConfig.expand) {
167 tests.forEach(test => this._logTest(test, indentLevel));
168 } else {
169 const summedTests = tests.reduce(
170 (result, test) => {
171 if (test.status === 'pending') {
172 result.pending.push(test);
173 } else if (test.status === 'todo') {
174 result.todo.push(test);
175 } else {
176 this._logTest(test, indentLevel);
177 }
178
179 return result;
180 },
181 {
182 pending: [],
183 todo: []
184 }
185 );
186
187 if (summedTests.pending.length > 0) {
188 summedTests.pending.forEach(this._logTodoOrPendingTest(indentLevel));
189 }
190
191 if (summedTests.todo.length > 0) {
192 summedTests.todo.forEach(this._logTodoOrPendingTest(indentLevel));
193 }
194 }
195 }
196
197 _logTodoOrPendingTest(indentLevel) {
198 return test => {
199 const printedTestStatus =
200 test.status === 'pending' ? 'skipped' : test.status;
201
202 const icon = this._getIcon(test.status);
203
204 const text = _chalk().default.dim(`${printedTestStatus} ${test.title}`);
205
206 this._logLine(`${icon} ${text}`, indentLevel);
207 };
208 }
209
210 _logLine(str, indentLevel) {
211 const indentation = ' '.repeat(indentLevel || 0);
212 this.log(indentation + (str || ''));
213 }
214}
215
216exports.default = VerboseReporter;