Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80606 views
1
/**
2
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
3
*
4
* This source code is licensed under the BSD-style license found in the
5
* LICENSE file in the root directory of this source tree. An additional grant
6
* of patent rights can be found in the PATENTS file in the same directory.
7
*/
8
'use strict';
9
10
var colors = require('./lib/colors');
11
var formatFailureMessage = require('./lib/utils').formatFailureMessage;
12
var path = require('path');
13
14
var FAIL_COLOR = colors.RED_BG + colors.BOLD;
15
var PASS_COLOR = colors.GREEN_BG + colors.BOLD;
16
var TEST_NAME_COLOR = colors.BOLD;
17
18
function DefaultTestReporter(customProcess) {
19
this._process = customProcess || process;
20
}
21
22
DefaultTestReporter.prototype.log = function(str) {
23
this._process.stdout.write(str + '\n');
24
};
25
26
DefaultTestReporter.prototype.onRunStart =
27
function(config, aggregatedResults) {
28
this._config = config;
29
this._printWaitingOn(aggregatedResults);
30
};
31
32
DefaultTestReporter.prototype.onTestResult =
33
function(config, testResult, aggregatedResults) {
34
this._clearWaitingOn();
35
36
var pathStr =
37
config.rootDir
38
? path.relative(config.rootDir, testResult.testFilePath)
39
: testResult.testFilePath;
40
41
if (testResult.testExecError) {
42
this.log(this._getResultHeader(false, pathStr));
43
this.log(testResult.testExecError);
44
return false;
45
}
46
47
var allTestsPassed = testResult.numFailingTests === 0;
48
49
var testRunTime =
50
testResult.perfStats
51
? (testResult.perfStats.end - testResult.perfStats.start) / 1000
52
: null;
53
54
var testRunTimeString = '(' + testRunTime + 's)';
55
if (testRunTime > 2.5) {
56
testRunTimeString = this._formatMsg(testRunTimeString, FAIL_COLOR);
57
}
58
59
/*
60
if (config.collectCoverage) {
61
// TODO: Find a nice pretty way to print this out
62
}
63
*/
64
65
this.log(this._getResultHeader(allTestsPassed, pathStr, [
66
testRunTimeString
67
]));
68
69
testResult.logMessages.forEach(this._printConsoleMessage.bind(this));
70
71
if (!allTestsPassed) {
72
this.log(formatFailureMessage(testResult, /*color*/!config.noHighlight));
73
}
74
75
this._printWaitingOn(aggregatedResults);
76
};
77
78
DefaultTestReporter.prototype.onRunComplete =
79
function (config, aggregatedResults) {
80
var numFailedTests = aggregatedResults.numFailedTests;
81
var numPassedTests = aggregatedResults.numPassedTests;
82
var numTotalTests = aggregatedResults.numTotalTests;
83
var runTime = aggregatedResults.runTime;
84
85
if (numTotalTests === 0) {
86
return;
87
}
88
89
var results = '';
90
if (numFailedTests) {
91
results += this._formatMsg(
92
numFailedTests + ' test' + (numFailedTests === 1 ? '' : 's') + ' failed',
93
colors.RED + colors.BOLD
94
);
95
results += ', ';
96
}
97
results += this._formatMsg(
98
numPassedTests + ' test' + (numPassedTests === 1 ? '' : 's') + ' passed',
99
colors.GREEN + colors.BOLD
100
);
101
results += ' (' + numTotalTests + ' total)';
102
103
this.log(results);
104
this.log('Run time: ' + runTime + 's');
105
};
106
107
DefaultTestReporter.prototype._printConsoleMessage = function(msg) {
108
switch (msg.type) {
109
case 'dir':
110
case 'log':
111
this._process.stdout.write(msg.data);
112
break;
113
case 'warn':
114
this._process.stderr.write(
115
this._formatMsg(msg.data, colors.YELLOW)
116
);
117
break;
118
case 'error':
119
this._process.stderr.write(
120
this._formatMsg(msg.data, colors.RED)
121
);
122
break;
123
default:
124
throw new Error('Unknown console message type!: ' + msg.type);
125
}
126
};
127
128
DefaultTestReporter.prototype._clearWaitingOn = function() {
129
// Don't write special chars in noHighlight mode
130
// to get clean output for logs.
131
var command = this._config.noHighlight
132
? '\n'
133
: '\r\x1B[K';
134
this._process.stdout.write(command);
135
};
136
137
DefaultTestReporter.prototype._formatMsg = function(msg, color) {
138
if (this._config.noHighlight) {
139
return msg;
140
}
141
return colors.colorize(msg, color);
142
};
143
144
DefaultTestReporter.prototype._getResultHeader =
145
function (passed, testName, columns) {
146
var passFailTag = passed
147
? this._formatMsg(' PASS ', PASS_COLOR)
148
: this._formatMsg(' FAIL ', FAIL_COLOR);
149
150
return [
151
passFailTag,
152
this._formatMsg(testName, TEST_NAME_COLOR)
153
].concat(columns || []).join(' ');
154
};
155
156
DefaultTestReporter.prototype._printWaitingOn = function(aggregatedResults) {
157
var completedTests =
158
aggregatedResults.numPassedTests +
159
aggregatedResults.numFailedTests;
160
var remainingTests = aggregatedResults.numTotalTests - completedTests;
161
if (remainingTests > 0) {
162
var pluralTests = remainingTests === 1 ? 'test' : 'tests';
163
this._process.stdout.write(
164
this._formatMsg(
165
'Waiting on ' + remainingTests + ' ' + pluralTests + '...',
166
colors.GRAY + colors.BOLD
167
)
168
);
169
}
170
};
171
172
module.exports = DefaultTestReporter;
173
174