-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
default_reporter.ts
201 lines (178 loc) · 5.32 KB
/
default_reporter.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {Config} from '@jest/types';
import {AggregatedResult, TestResult} from '@jest/test-result';
import {clearLine, getConsoleOutput, isInteractive} from 'jest-util';
import chalk from 'chalk';
import {Test, ReporterOnStartOptions} from './types';
import BaseReporter from './base_reporter';
import Status from './Status';
import getResultHeader from './get_result_header';
import getSnapshotStatus from './get_snapshot_status';
type write = (chunk: string, enc?: any, cb?: () => void) => boolean;
type FlushBufferedOutput = () => void;
const TITLE_BULLET = chalk.bold('\u25cf ');
export default class DefaultReporter extends BaseReporter {
private _clear: string; // ANSI clear sequence for the last printed status
private _err: write;
protected _globalConfig: Config.GlobalConfig;
private _out: write;
private _status: Status;
private _bufferedOutput: Set<FlushBufferedOutput>;
constructor(globalConfig: Config.GlobalConfig) {
super();
this._globalConfig = globalConfig;
this._clear = '';
this._out = process.stdout.write.bind(process.stdout);
this._err = process.stderr.write.bind(process.stderr);
this._status = new Status();
this._bufferedOutput = new Set();
this._wrapStdio(process.stdout);
this._wrapStdio(process.stderr);
this._status.onChange(() => {
this._clearStatus();
this._printStatus();
});
}
private _wrapStdio(stream: NodeJS.WritableStream | NodeJS.WriteStream) {
const originalWrite = stream.write;
let buffer: Array<string> = [];
let timeout: NodeJS.Timeout | null = null;
const flushBufferedOutput = () => {
const string = buffer.join('');
buffer = [];
// This is to avoid conflicts between random output and status text
this._clearStatus();
if (string) {
originalWrite.call(stream, string);
}
this._printStatus();
this._bufferedOutput.delete(flushBufferedOutput);
};
this._bufferedOutput.add(flushBufferedOutput);
const debouncedFlush = () => {
// If the process blows up no errors would be printed.
// There should be a smart way to buffer stderr, but for now
// we just won't buffer it.
if (stream === process.stderr) {
flushBufferedOutput();
} else {
if (!timeout) {
timeout = setTimeout(() => {
flushBufferedOutput();
timeout = null;
}, 100);
}
}
};
stream.write = (chunk: string) => {
buffer.push(chunk);
debouncedFlush();
return true;
};
}
// Don't wait for the debounced call and flush all output immediately.
forceFlushBufferedOutput() {
for (const flushBufferedOutput of this._bufferedOutput) {
flushBufferedOutput();
}
}
private _clearStatus() {
if (isInteractive) {
if (this._globalConfig.useStderr) {
this._err(this._clear);
} else {
this._out(this._clear);
}
}
}
private _printStatus() {
const {content, clear} = this._status.get();
this._clear = clear;
if (isInteractive) {
if (this._globalConfig.useStderr) {
this._err(content);
} else {
this._out(content);
}
}
}
onRunStart(
aggregatedResults: AggregatedResult,
options: ReporterOnStartOptions,
) {
this._status.runStarted(aggregatedResults, options);
}
onTestStart(test: Test) {
this._status.testStarted(test.path, test.context.config);
}
onRunComplete() {
this.forceFlushBufferedOutput();
this._status.runFinished();
process.stdout.write = this._out;
process.stderr.write = this._err;
clearLine(process.stderr);
}
onTestResult(
test: Test,
testResult: TestResult,
aggregatedResults: AggregatedResult,
) {
this.testFinished(test.context.config, testResult, aggregatedResults);
if (!testResult.skipped) {
this.printTestFileHeader(
testResult.testFilePath,
test.context.config,
testResult,
);
this.printTestFileFailureMessage(
testResult.testFilePath,
test.context.config,
testResult,
);
}
this.forceFlushBufferedOutput();
}
testFinished(
config: Config.ProjectConfig,
testResult: TestResult,
aggregatedResults: AggregatedResult,
) {
this._status.testFinished(config, testResult, aggregatedResults);
}
printTestFileHeader(
_testPath: Config.Path,
config: Config.ProjectConfig,
result: TestResult,
) {
this.log(getResultHeader(result, this._globalConfig, config));
if (result.console) {
this.log(
' ' +
TITLE_BULLET +
'Console\n\n' +
getConsoleOutput(
config.cwd,
!!this._globalConfig.verbose,
result.console,
),
);
}
}
printTestFileFailureMessage(
_testPath: Config.Path,
_config: Config.ProjectConfig,
result: TestResult,
) {
if (result.failureMessage) {
this.log(result.failureMessage);
}
const didUpdate = this._globalConfig.updateSnapshot === 'all';
const snapshotStatuses = getSnapshotStatus(result.snapshot, didUpdate);
snapshotStatuses.forEach(this.log);
}
}