Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

allow override of default test suite name in xunit reporter; closes #2628

Nikolaos Georgioude682508930c
diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js
index ecfc8b23..7d98815c 100644
--- a/lib/reporters/xunit.js
+++ b/lib/reporters/xunit.js
@@ -43,14 +43,29 @@ function XUnit (runner, options) {
var tests = [];
var self = this;
- if (options && options.reporterOptions && options.reporterOptions.output) {
- if (!fs.createWriteStream) {
- throw new Error('file output not supported in browser');
+ // the name of the test suite, as it will appear in the resulting XML file
+ var suiteName;
+
+ // the default name of the test suite if none is provided
+ var DEFAULT_SUITE_NAME = 'Mocha Tests';
+
+ if (options && options.reporterOptions) {
+ if (options.reporterOptions.output) {
+ if (!fs.createWriteStream) {
+ throw new Error('file output not supported in browser');
+ }
+
+ mkdirp.sync(path.dirname(options.reporterOptions.output));
+ self.fileStream = fs.createWriteStream(options.reporterOptions.output);
}
- mkdirp.sync(path.dirname(options.reporterOptions.output));
- self.fileStream = fs.createWriteStream(options.reporterOptions.output);
+
+ // get the suite name from the reporter options (if provided)
+ suiteName = options.reporterOptions.suiteName;
}
+ // fall back to the default suite name
+ suiteName = suiteName || DEFAULT_SUITE_NAME;
+
runner.on('pending', function (test) {
tests.push(test);
});
@@ -65,7 +80,7 @@ function XUnit (runner, options) {
runner.on('end', function () {
self.write(tag('testsuite', {
- name: 'Mocha Tests',
+ name: suiteName,
tests: stats.tests,
failures: stats.failures,
errors: stats.failures,
diff --git a/test/reporters/xunit.spec.js b/test/reporters/xunit.spec.js
index 8b547767..38ca54b2 100644
--- a/test/reporters/xunit.spec.js
+++ b/test/reporters/xunit.spec.js
@@ -3,6 +3,7 @@
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
+var assert = require('assert');
var reporters = require('../../').reporters;
var XUnit = reporters.XUnit;
@@ -313,4 +314,68 @@ describe('XUnit reporter', function () {
});
});
});
+
+ describe('custom suite name', function () {
+ // capture the events that the reporter subscribes to
+ var events;
+
+ // the runner parameter of the reporter
+ var runner;
+
+ // capture output lines (will contain the resulting XML of the xunit reporter)
+ var lines;
+
+ // the file stream into which the xunit reporter will write into
+ var fileStream;
+
+ beforeEach(function () {
+ events = {};
+
+ runner = {
+ on: function (eventName, eventHandler) {
+ // capture the event handler
+ events[eventName] = eventHandler;
+ }
+ };
+
+ lines = [];
+ fileStream = {
+ write: function (line) {
+ // capture the output lines
+ lines.push(line);
+ }
+ };
+ });
+
+ it('should use "Mocha Tests" as the suite name if no custom name is provided', function () {
+ // arrange
+ var xunit = new XUnit(runner);
+ xunit.fileStream = fileStream;
+
+ // act (trigger the end event to force xunit reporter to write the output)
+ events['end']();
+
+ // assert
+ assert(lines[0].indexOf('Mocha Tests') >= 0, 'it should contain the text "Mocha Tests"');
+ });
+
+ it('should use the custom suite name as the suite name when provided in the reporter options', function () {
+ // arrange
+ var options = {
+ reporterOptions: {
+ // this time, with a custom suite name
+ suiteName: 'Mocha Is Great!'
+ }
+ };
+
+ var xunit = new XUnit(runner, options);
+ xunit.fileStream = fileStream;
+
+ // act (trigger the end event to force xunit reporter to write the output)
+ events['end']();
+
+ // assert
+ assert(lines[0].indexOf('<testsuite name="Mocha Is Great!"') === 0, '"' + lines[0] + '" should contain the text "Mocha Is Great"');
+ });
+ });
});