aboutsummaryrefslogtreecommitdiff
path: root/node_modules/ava/lib/reporters/verbose.js
blob: cd47683e86cc19a0a8a4f516f640117927c35eaf (plain)
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
'use strict';
const indentString = require('indent-string');
const prettyMs = require('pretty-ms');
const figures = require('figures');
const chalk = require('chalk');
const plur = require('plur');
const trimOffNewlines = require('trim-off-newlines');
const extractStack = require('../extract-stack');
const codeExcerpt = require('../code-excerpt');
const colors = require('../colors');
const formatSerializedError = require('./format-serialized-error');
const improperUsageMessages = require('./improper-usage-messages');

class VerboseReporter {
	constructor(options) {
		this.options = Object.assign({}, options);

		chalk.enabled = this.options.color;
		for (const key of Object.keys(colors)) {
			colors[key].enabled = this.options.color;
		}
	}
	start() {
		return '';
	}
	test(test, runStatus) {
		if (test.error) {
			return '  ' + colors.error(figures.cross) + ' ' + test.title + ' ' + colors.error(test.error.message);
		}

		if (test.todo) {
			return '  ' + colors.todo('- ' + test.title);
		} else if (test.skip) {
			return '  ' + colors.skip('- ' + test.title);
		}

		if (test.failing) {
			return '  ' + colors.error(figures.tick) + ' ' + colors.error(test.title);
		}

		if (runStatus.fileCount === 1 && runStatus.testCount === 1 && test.title === '[anonymous]') {
			return undefined;
		}

		// Display duration only over a threshold
		const threshold = 100;
		const duration = test.duration > threshold ? colors.duration(' (' + prettyMs(test.duration) + ')') : '';

		return '  ' + colors.pass(figures.tick) + ' ' + test.title + duration;
	}
	unhandledError(err) {
		if (err.type === 'exception' && err.name === 'AvaError') {
			return colors.error('  ' + figures.cross + ' ' + err.message);
		}

		const types = {
			rejection: 'Unhandled Rejection',
			exception: 'Uncaught Exception'
		};

		let output = colors.error(types[err.type] + ':', err.file) + '\n';

		if (err.stack) {
			output += '  ' + colors.stack(err.stack) + '\n';
		} else {
			output += '  ' + colors.stack(JSON.stringify(err)) + '\n';
		}

		output += '\n';

		return output;
	}
	finish(runStatus) {
		let output = '';

		const lines = [
			runStatus.failCount > 0 ?
				'  ' + colors.error(runStatus.failCount, plur('test', runStatus.failCount), 'failed') :
				'  ' + colors.pass(runStatus.passCount, plur('test', runStatus.passCount), 'passed'),
			runStatus.knownFailureCount > 0 ? '  ' + colors.error(runStatus.knownFailureCount, plur('known failure', runStatus.knownFailureCount)) : '',
			runStatus.skipCount > 0 ? '  ' + colors.skip(runStatus.skipCount, plur('test', runStatus.skipCount), 'skipped') : '',
			runStatus.todoCount > 0 ? '  ' + colors.todo(runStatus.todoCount, plur('test', runStatus.todoCount), 'todo') : '',
			runStatus.rejectionCount > 0 ? '  ' + colors.error(runStatus.rejectionCount, 'unhandled', plur('rejection', runStatus.rejectionCount)) : '',
			runStatus.exceptionCount > 0 ? '  ' + colors.error(runStatus.exceptionCount, 'uncaught', plur('exception', runStatus.exceptionCount)) : '',
			runStatus.previousFailCount > 0 ? '  ' + colors.error(runStatus.previousFailCount, 'previous', plur('failure', runStatus.previousFailCount), 'in test files that were not rerun') : ''
		].filter(Boolean);

		if (lines.length > 0) {
			lines[0] += ' ' + chalk.gray.dim('[' + new Date().toLocaleTimeString('en-US', {hour12: false}) + ']');
			output += lines.join('\n') + '\n';
		}

		if (runStatus.knownFailureCount > 0) {
			runStatus.knownFailures.forEach(test => {
				output += '\n\n  ' + colors.error(test.title) + '\n';
			});
		}

		output += '\n';
		if (runStatus.failCount > 0) {
			runStatus.tests.forEach(test => {
				if (!test.error) {
					return;
				}

				output += '  ' + colors.title(test.title) + '\n';
				if (test.error.source) {
					output += '  ' + colors.errorSource(test.error.source.file + ':' + test.error.source.line) + '\n';

					const excerpt = codeExcerpt(test.error.source, {maxWidth: process.stdout.columns});
					if (excerpt) {
						output += '\n' + indentString(excerpt, 2) + '\n';
					}
				}

				if (test.error.avaAssertionError) {
					const result = formatSerializedError(test.error);
					if (result.printMessage) {
						output += '\n' + indentString(test.error.message, 2) + '\n';
					}

					if (result.formatted) {
						output += '\n' + indentString(result.formatted, 2) + '\n';
					}

					const message = improperUsageMessages.forError(test.error);
					if (message) {
						output += '\n' + indentString(message, 2) + '\n';
					}
				} else if (test.error.message) {
					output += '\n' + indentString(test.error.message, 2) + '\n';
				}

				if (test.error.stack) {
					const extracted = extractStack(test.error.stack);
					if (extracted.includes('\n')) {
						output += '\n' + indentString(colors.errorStack(extracted), 2) + '\n';
					}
				}

				output += '\n\n\n';
			});
		}

		if (runStatus.failFastEnabled === true && runStatus.remainingCount > 0 && runStatus.failCount > 0) {
			const remaining = 'At least ' + runStatus.remainingCount + ' ' + plur('test was', 'tests were', runStatus.remainingCount) + ' skipped.';
			output += '  ' + colors.information('`--fail-fast` is on. ' + remaining) + '\n\n';
		}

		if (runStatus.hasExclusive === true && runStatus.remainingCount > 0) {
			output += '  ' + colors.information('The .only() modifier is used in some tests.', runStatus.remainingCount, plur('test', runStatus.remainingCount), plur('was', 'were', runStatus.remainingCount), 'not run');
		}

		return '\n' + trimOffNewlines(output) + '\n';
	}
	section() {
		return chalk.gray.dim('\u2500'.repeat(process.stdout.columns || 80));
	}
	write(str) {
		console.error(str);
	}
	stdout(data) {
		process.stderr.write(data);
	}
	stderr(data) {
		process.stderr.write(data);
	}
}

module.exports = VerboseReporter;