Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit b5568d9

Browse files
ngeorboneskull
authored andcommitted
allow override of default test suite name in xunit reporter; closes mochajs#2628
* Fixing issue 2628 allowing to override the default test suite name of the xunit reporter. * Fixing indentation * Linting fixes.
1 parent a5f0ec9 commit b5568d9

File tree

2 files changed

+86
-6
lines changed

2 files changed

+86
-6
lines changed

lib/reporters/xunit.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,29 @@ function XUnit (runner, options) {
4343
var tests = [];
4444
var self = this;
4545

46-
if (options && options.reporterOptions && options.reporterOptions.output) {
47-
if (!fs.createWriteStream) {
48-
throw new Error('file output not supported in browser');
46+
// the name of the test suite, as it will appear in the resulting XML file
47+
var suiteName;
48+
49+
// the default name of the test suite if none is provided
50+
var DEFAULT_SUITE_NAME = 'Mocha Tests';
51+
52+
if (options && options.reporterOptions) {
53+
if (options.reporterOptions.output) {
54+
if (!fs.createWriteStream) {
55+
throw new Error('file output not supported in browser');
56+
}
57+
58+
mkdirp.sync(path.dirname(options.reporterOptions.output));
59+
self.fileStream = fs.createWriteStream(options.reporterOptions.output);
4960
}
50-
mkdirp.sync(path.dirname(options.reporterOptions.output));
51-
self.fileStream = fs.createWriteStream(options.reporterOptions.output);
61+
62+
// get the suite name from the reporter options (if provided)
63+
suiteName = options.reporterOptions.suiteName;
5264
}
5365

66+
// fall back to the default suite name
67+
suiteName = suiteName || DEFAULT_SUITE_NAME;
68+
5469
runner.on('pending', function (test) {
5570
tests.push(test);
5671
});
@@ -65,7 +80,7 @@ function XUnit (runner, options) {
6580

6681
runner.on('end', function () {
6782
self.write(tag('testsuite', {
68-
name: 'Mocha Tests',
83+
name: suiteName,
6984
tests: stats.tests,
7085
failures: stats.failures,
7186
errors: stats.failures,

test/reporters/xunit.spec.js

+65
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var fs = require('fs');
44
var mkdirp = require('mkdirp');
55
var path = require('path');
6+
var assert = require('assert');
67
var reporters = require('../../').reporters;
78
var XUnit = reporters.XUnit;
89

@@ -313,4 +314,68 @@ describe('XUnit reporter', function () {
313314
});
314315
});
315316
});
317+
318+
describe('custom suite name', function () {
319+
// capture the events that the reporter subscribes to
320+
var events;
321+
322+
// the runner parameter of the reporter
323+
var runner;
324+
325+
// capture output lines (will contain the resulting XML of the xunit reporter)
326+
var lines;
327+
328+
// the file stream into which the xunit reporter will write into
329+
var fileStream;
330+
331+
beforeEach(function () {
332+
events = {};
333+
334+
runner = {
335+
on: function (eventName, eventHandler) {
336+
// capture the event handler
337+
events[eventName] = eventHandler;
338+
}
339+
};
340+
341+
lines = [];
342+
fileStream = {
343+
write: function (line) {
344+
// capture the output lines
345+
lines.push(line);
346+
}
347+
};
348+
});
349+
350+
it('should use "Mocha Tests" as the suite name if no custom name is provided', function () {
351+
// arrange
352+
var xunit = new XUnit(runner);
353+
xunit.fileStream = fileStream;
354+
355+
// act (trigger the end event to force xunit reporter to write the output)
356+
events['end']();
357+
358+
// assert
359+
assert(lines[0].indexOf('Mocha Tests') >= 0, 'it should contain the text "Mocha Tests"');
360+
});
361+
362+
it('should use the custom suite name as the suite name when provided in the reporter options', function () {
363+
// arrange
364+
var options = {
365+
reporterOptions: {
366+
// this time, with a custom suite name
367+
suiteName: 'Mocha Is Great!'
368+
}
369+
};
370+
371+
var xunit = new XUnit(runner, options);
372+
xunit.fileStream = fileStream;
373+
374+
// act (trigger the end event to force xunit reporter to write the output)
375+
events['end']();
376+
377+
// assert
378+
assert(lines[0].indexOf('<testsuite name="Mocha Is Great!"') === 0, '"' + lines[0] + '" should contain the text "Mocha Is Great"');
379+
});
380+
});
316381
});

0 commit comments

Comments
 (0)