aboutsummaryrefslogtreecommitdiff
path: root/node_modules/ava/lib/reporters/tap.js
blob: 5ef8a23e397820d0a10491664ac64f6647b06052 (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
'use strict';
const format = require('util').format;
const indentString = require('indent-string');
const stripAnsi = require('strip-ansi');
const yaml = require('js-yaml');

function dumpError(error, includeMessage) {
	const obj = Object.assign({}, error.object);
	if (error.name) {
		obj.name = error.name;
	}
	if (includeMessage && error.message) {
		obj.message = error.message;
	}

	if (error.avaAssertionError) {
		if (error.assertion) {
			obj.assertion = error.assertion;
		}
		if (error.operator) {
			obj.operator = error.operator;
		}
		if (error.values.length > 0) {
			obj.values = error.values.reduce((acc, value) => {
				acc[value.label] = stripAnsi(value.formatted);
				return acc;
			}, {});
		}
	}

	if (error.stack) {
		obj.at = error.stack.split('\n')[0];
	}

	return `  ---\n${indentString(yaml.safeDump(obj).trim(), 4)}\n  ...`;
}

class TapReporter {
	constructor() {
		this.i = 0;
	}

	start() {
		return 'TAP version 13';
	}

	test(test) {
		const output = [];

		let directive = '';
		const passed = test.todo ? 'not ok' : 'ok';

		if (test.todo) {
			directive = '# TODO';
		} else if (test.skip) {
			directive = '# SKIP';
		}

		const title = stripAnsi(test.title);

		const appendLogs = () => {
			if (test.logs) {
				test.logs.forEach(log => {
					const logLines = indentString(log, 4);
					const logLinesWithLeadingFigure = logLines.replace(
						/^ {4}/,
						'  * '
					);

					output.push(logLinesWithLeadingFigure);
				});
			}
		};

		output.push(`# ${title}`);

		if (test.error) {
			output.push(format('not ok %d - %s', ++this.i, title));
			appendLogs();
			output.push(dumpError(test.error, true));
		} else {
			output.push(format('%s %d - %s %s', passed, ++this.i, title, directive).trim());
			appendLogs();
		}

		return output.join('\n');
	}

	unhandledError(err) {
		const output = [
			`# ${err.message}`,
			format('not ok %d - %s', ++this.i, err.message)
		];
		// AvaErrors don't have stack traces
		if (err.type !== 'exception' || err.name !== 'AvaError') {
			output.push(dumpError(err, false));
		}

		return output.join('\n');
	}

	finish(runStatus) {
		const output = [
			'',
			'1..' + (runStatus.passCount + runStatus.failCount + runStatus.skipCount),
			'# tests ' + (runStatus.passCount + runStatus.failCount + runStatus.skipCount),
			'# pass ' + runStatus.passCount
		];

		if (runStatus.skipCount > 0) {
			output.push(`# skip ${runStatus.skipCount}`);
		}

		output.push('# fail ' + (runStatus.failCount + runStatus.rejectionCount + runStatus.exceptionCount), '');

		return output.join('\n');
	}

	write(str) {
		console.log(str);
	}

	stdout(data) {
		process.stderr.write(data);
	}

	stderr(data) {
		this.stdout(data);
	}
}

module.exports = TapReporter;