Debian Codegraph @

lib/reporters/tap.js

raw symbols SCIP
1 "use strict";
2
3 /**
4 * @typedef {import('../runner.js')} Runner
5 * @typedef {import('../test.js')} Test
6 */
7
8 /**
9 * @module TAP
10 */
11 /**
12 * Module dependencies.
13 */
14
15 var util = require("node:util");
16 var Base = require("./base");
17 var constants = require("../runner").constants;
18 var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
19 var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
20 var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
21 var EVENT_RUN_END = constants.EVENT_RUN_END;
22 var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
23 var EVENT_TEST_END = constants.EVENT_TEST_END;
24 var inherits = require("../utils").inherits;
25 var sprintf = util.format;
26
27 /**
28 * Expose `TAP`.
29 */
30
31 exports = module.exports = TAP;
32
33 /**
34 * Constructs a new `TAP` reporter instance.
35 *
36 * @public
37 * @class
38 * @memberof Mocha.reporters
39 * @extends Mocha.reporters.Base
40 * @param {Runner} runner - Instance triggers reporter actions.
41 * @param {Object} [options] - runner options
42 */
43 function TAP(runner, options) {
44 Base.call(this, runner, options);
45
46 var self = this;
47 var n = 1;
48
49 var tapVersion = "12";
50 if (options && options.reporterOptions) {
51 if (options.reporterOptions.tapVersion) {
52 tapVersion = options.reporterOptions.tapVersion.toString();
53 }
54 }
55
56 this._producer = createProducer(tapVersion);
57
58 runner.once(EVENT_RUN_BEGIN, function () {
59 self._producer.writeVersion();
60 });
61
62 runner.on(EVENT_TEST_END, function () {
63 ++n;
64 });
65
66 runner.on(EVENT_TEST_PENDING, function (test) {
67 self._producer.writePending(n, test);
68 });
69
70 runner.on(EVENT_TEST_PASS, function (test) {
71 self._producer.writePass(n, test);
72 });
73
74 runner.on(EVENT_TEST_FAIL, function (test, err) {
75 self._producer.writeFail(n, test, err);
76 });
77
78 runner.once(EVENT_RUN_END, function () {
79 self._producer.writeEpilogue(runner.stats);
80 });
81 }
82
83 /**
84 * Inherit from `Base.prototype`.
85 */
86 inherits(TAP, Base);
87
88 /**
89 * Returns a TAP-safe title of `test`.
90 *
91 * @private
92 * @param {Test} test - Test instance.
93 * @return {String} title with any hash character removed
94 */
95 function title(test) {
96 return test.fullTitle().replace(/#/g, "");
97 }
98
99 /**
100 * Writes newline-terminated formatted string to reporter output stream.
101 *
102 * @private
103 * @param {string} format - `printf`-like format string
104 * @param {...*} [varArgs] - Format string arguments
105 */
106 function println() {
107 var vargs = Array.from(arguments);
108 vargs[0] += "\n";
109 process.stdout.write(sprintf.apply(null, vargs));
110 }
111
112 /**
113 * Returns a `tapVersion`-appropriate TAP producer instance, if possible.
114 *
115 * @private
116 * @param {string} tapVersion - Version of TAP specification to produce.
117 * @returns {TAPProducer} specification-appropriate instance
118 * @throws {Error} if specification version has no associated producer.
119 */
120 function createProducer(tapVersion) {
121 var producers = {
122 12: new TAP12Producer(),
123 13: new TAP13Producer(),
124 };
125 var producer = producers[tapVersion];
126
127 if (!producer) {
128 throw new Error(
129 "invalid or unsupported TAP version: " + JSON.stringify(tapVersion),
130 );
131 }
132
133 return producer;
134 }
135
136 /**
137 * @summary
138 * Constructs a new TAPProducer.
139 *
140 * @description
141 * <em>Only</em> to be used as an abstract base class.
142 *
143 * @private
144 * @constructor
145 */
146 function TAPProducer() {}
147
148 /**
149 * Writes the TAP version to reporter output stream.
150 *
151 * @abstract
152 */
153 TAPProducer.prototype.writeVersion = function () {};
154
155 /**
156 * Writes the plan to reporter output stream.
157 *
158 * @abstract
159 * @param {number} ntests - Number of tests that are planned to run.
160 */
161 TAPProducer.prototype.writePlan = function (ntests) {
162 println("%d..%d", 1, ntests);
163 };
164
165 /**
166 * Writes that test passed to reporter output stream.
167 *
168 * @abstract
169 * @param {number} n - Index of test that passed.
170 * @param {Test} test - Instance containing test information.
171 */
172 TAPProducer.prototype.writePass = function (n, test) {
173 println("ok %d %s", n, title(test));
174 };
175
176 /**
177 * Writes that test was skipped to reporter output stream.
178 *
179 * @abstract
180 * @param {number} n - Index of test that was skipped.
181 * @param {Test} test - Instance containing test information.
182 */
183 TAPProducer.prototype.writePending = function (n, test) {
184 println("ok %d %s # SKIP -", n, title(test));
185 };
186
187 /**
188 * Writes that test failed to reporter output stream.
189 *
190 * @abstract
191 * @param {number} n - Index of test that failed.
192 * @param {Test} test - Instance containing test information.
193 */
194 TAPProducer.prototype.writeFail = function (n, test) {
195 println("not ok %d %s", n, title(test));
196 };
197
198 /**
199 * Writes the summary epilogue to reporter output stream.
200 *
201 * @abstract
202 * @param {Object} stats - Object containing run statistics.
203 */
204 TAPProducer.prototype.writeEpilogue = function (stats) {
205 // :TBD: Why is this not counting pending tests?
206 println("# tests " + (stats.passes + stats.failures));
207 println("# pass " + stats.passes);
208 // :TBD: Why are we not showing pending results?
209 println("# fail " + stats.failures);
210 this.writePlan(stats.passes + stats.failures + stats.pending);
211 };
212
213 /**
214 * @summary
215 * Constructs a new TAP12Producer.
216 *
217 * @description
218 * Produces output conforming to the TAP12 specification.
219 *
220 * @private
221 * @constructor
222 * @extends TAPProducer
223 * @see {@link https://testanything.org/tap-specification.html|Specification}
224 */
225 function TAP12Producer() {
226 /**
227 * Writes that test failed to reporter output stream, with error formatting.
228 * @override
229 */
230 this.writeFail = function (n, test, err) {
231 TAPProducer.prototype.writeFail.call(this, n, test, err);
232 if (err.message) {
233 println(err.message.replace(/^/gm, " "));
234 }
235 if (err.stack) {
236 println(err.stack.replace(/^/gm, " "));
237 }
238 };
239 }
240
241 /**
242 * Inherit from `TAPProducer.prototype`.
243 */
244 inherits(TAP12Producer, TAPProducer);
245
246 /**
247 * @summary
248 * Constructs a new TAP13Producer.
249 *
250 * @description
251 * Produces output conforming to the TAP13 specification.
252 *
253 * @private
254 * @constructor
255 * @extends TAPProducer
256 * @see {@link https://testanything.org/tap-version-13-specification.html|Specification}
257 */
258 function TAP13Producer() {
259 /**
260 * Writes the TAP version to reporter output stream.
261 * @override
262 */
263 this.writeVersion = function () {
264 println("TAP version 13");
265 };
266
267 /**
268 * Writes that test failed to reporter output stream, with error formatting.
269 * @override
270 */
271 this.writeFail = function (n, test, err) {
272 TAPProducer.prototype.writeFail.call(this, n, test, err);
273 var emitYamlBlock = err.message != null || err.stack != null;
274 if (emitYamlBlock) {
275 println(indent(1) + "---");
276 if (err.message) {
277 println(indent(2) + "message: |-");
278 println(err.message.replace(/^/gm, indent(3)));
279 }
280 if (err.stack) {
281 println(indent(2) + "stack: |-");
282 println(err.stack.replace(/^/gm, indent(3)));
283 }
284 println(indent(1) + "...");
285 }
286 };
287
288 function indent(level) {
289 return Array(level + 1).join(" ");
290 }
291 }
292
293 /**
294 * Inherit from `TAPProducer.prototype`.
295 */
296 inherits(TAP13Producer, TAPProducer);
297
298 TAP.description = "TAP-compatible output";