Debian Codegraph @

lib/reporters/xunit.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 XUnit
10 */
11 /**
12 * Module dependencies.
13 */
14
15 var Base = require("./base");
16 var utils = require("../utils");
17 var fs = require("node:fs");
18 var path = require("node:path");
19 var errors = require("../errors");
20 var createUnsupportedError = errors.createUnsupportedError;
21 var constants = require("../runner").constants;
22 var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
23 var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
24 var EVENT_RUN_END = constants.EVENT_RUN_END;
25 var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
26 var STATE_FAILED = require("../runnable").constants.STATE_FAILED;
27 var inherits = utils.inherits;
28 var escape = utils.escape;
29
30 /**
31 * Save timer references to avoid Sinon interfering (see GH-237).
32 */
33 var Date = global.Date;
34
35 /**
36 * Expose `XUnit`.
37 */
38
39 exports = module.exports = XUnit;
40
41 /**
42 * Constructs a new `XUnit` reporter instance.
43 *
44 * @public
45 * @class
46 * @memberof Mocha.reporters
47 * @extends Mocha.reporters.Base
48 * @param {Runner} runner - Instance triggers reporter actions.
49 * @param {Object} [options] - runner options
50 */
51 function XUnit(runner, options) {
52 Base.call(this, runner, options);
53
54 var stats = this.stats;
55 var tests = [];
56 var self = this;
57
58 // the name of the test suite, as it will appear in the resulting XML file
59 var suiteName;
60
61 // the default name of the test suite if none is provided
62 var DEFAULT_SUITE_NAME = "Mocha Tests";
63
64 if (options && options.reporterOptions) {
65 if (options.reporterOptions.output) {
66 if (!fs.createWriteStream) {
67 throw createUnsupportedError("file output not supported in browser");
68 }
69
70 fs.mkdirSync(path.dirname(options.reporterOptions.output), {
71 recursive: true,
72 });
73 self.fileStream = fs.createWriteStream(options.reporterOptions.output);
74 }
75
76 // get the suite name from the reporter options (if provided)
77 suiteName = options.reporterOptions.suiteName;
78 }
79
80 // fall back to the default suite name
81 suiteName = suiteName || DEFAULT_SUITE_NAME;
82
83 runner.on(EVENT_TEST_PENDING, function (test) {
84 tests.push(test);
85 });
86
87 runner.on(EVENT_TEST_PASS, function (test) {
88 tests.push(test);
89 });
90
91 runner.on(EVENT_TEST_FAIL, function (test) {
92 tests.push(test);
93 });
94
95 runner.once(EVENT_RUN_END, function () {
96 self.write(
97 tag(
98 "testsuite",
99 {
100 name: suiteName,
101 tests: stats.tests,
102 failures: 0,
103 errors: stats.failures,
104 skipped: stats.tests - stats.failures - stats.passes,
105 timestamp: new Date().toUTCString(),
106 time: stats.duration / 1000 || 0,
107 },
108 false,
109 ),
110 );
111
112 tests.forEach(function (t) {
113 self.test(t, options);
114 });
115
116 self.write("</testsuite>");
117 });
118 }
119
120 /**
121 * Inherit from `Base.prototype`.
122 */
123 inherits(XUnit, Base);
124
125 /**
126 * Override done to close the stream (if it's a file).
127 *
128 * @param failures
129 * @param {Function} fn
130 */
131 XUnit.prototype.done = function (failures, fn) {
132 if (this.fileStream) {
133 this.fileStream.end(function () {
134 fn(failures);
135 });
136 } else {
137 fn(failures);
138 }
139 };
140
141 /**
142 * Write out the given line.
143 *
144 * @param {string} line
145 */
146 XUnit.prototype.write = function (line) {
147 if (this.fileStream) {
148 this.fileStream.write(line + "\n");
149 } else if (typeof process === "object" && process.stdout) {
150 process.stdout.write(line + "\n");
151 } else {
152 Base.consoleLog(line);
153 }
154 };
155
156 /**
157 * Output tag for the given `test.`
158 *
159 * @param {Test} test
160 */
161 XUnit.prototype.test = function (test, options) {
162 Base.useColors = false;
163
164 var attrs = {
165 classname: test.parent.fullTitle(),
166 name: test.title,
167 file: testFilePath(test.file, options),
168 time: test.duration / 1000 || 0,
169 };
170
171 if (test.state === STATE_FAILED) {
172 var err = test.err;
173 var diff =
174 !Base.hideDiff && Base.showDiff(err)
175 ? "\n" + Base.generateDiff(err.actual, err.expected)
176 : "";
177 this.write(
178 tag(
179 "testcase",
180 attrs,
181 false,
182 tag(
183 "failure",
184 {},
185 false,
186 escape(err.message) + escape(diff) + "\n" + escape(err.stack),
187 ),
188 ),
189 );
190 } else if (test.isPending()) {
191 this.write(tag("testcase", attrs, false, tag("skipped", {}, true)));
192 } else {
193 this.write(tag("testcase", attrs, true));
194 }
195 };
196
197 /**
198 * HTML tag helper.
199 *
200 * @param name
201 * @param attrs
202 * @param close
203 * @param content
204 * @return {string}
205 */
206 function tag(name, attrs, close, content) {
207 var end = close ? "/>" : ">";
208 var pairs = [];
209 var tag;
210
211 for (var key in attrs) {
212 if (Object.prototype.hasOwnProperty.call(attrs, key)) {
213 pairs.push(key + '="' + escape(attrs[key]) + '"');
214 }
215 }
216
217 tag = "<" + name + (pairs.length ? " " + pairs.join(" ") : "") + end;
218 if (content) {
219 tag += content + "</" + name + end;
220 }
221 return tag;
222 }
223
224 function testFilePath(filepath, options) {
225 if (
226 options &&
227 options.reporterOptions &&
228 options.reporterOptions.showRelativePaths
229 ) {
230 return path.relative(process.cwd(), filepath);
231 }
232
233 return filepath;
234 }
235
236 XUnit.description = "XUnit-compatible XML output";