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

Commit

Permalink
allow override of default test suite name in xunit reporter; closes m…
Browse files Browse the repository at this point in the history
…ochajs#2628

* Fixing issue 2628 allowing to override the default test suite name of the xunit reporter.

* Fixing indentation

* Linting fixes.
  • Loading branch information
ngeor authored and boneskull committed Sep 28, 2017
1 parent a5f0ec9 commit b5568d9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/reporters/xunit.js
Expand Up @@ -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);
});
Expand All @@ -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,
Expand Down
65 changes: 65 additions & 0 deletions test/reporters/xunit.spec.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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"');
});
});
});

0 comments on commit b5568d9

Please sign in to comment.