Skip to content

Commit

Permalink
Merge pull request #3 from passuied/feature/support-failure-element
Browse files Browse the repository at this point in the history
Add support for failure element based on xunit xml format
  • Loading branch information
john-ro committed Mar 17, 2020
2 parents db0adfb + 11e1027 commit 9a5ae60
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 25 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ Given a test with title 'test should behave like so @aid=EPM-DP-C1234 @sid=EPM-1
<test name="test should behave like so" aid="EPM-DP-C1234" sid="EPM-1234" type="Integration" />
```

## XUnit XML format
The generated test results file conforms to [XUnit's XML format](https://xunit.net/docs/format-xml-v2).

### `failure` element
In case of an error or failure during a test run, a `failure` element will be included as a child element with its corresponding `test` element. This element will contain information about the failed test.

```
<failure exception-type="Error">
<message><![CDATA[This test threw an error]]></message>
<stack-trace><![CDATA[Error: testing123
at Context.<anonymous> (example.spec.ts-1:1:1)]]></stack-trace>
</failure>
```

### Full configuration options

| Parameter | Effect |
Expand Down
33 changes: 24 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ function configureDefaults(options) {
return options;
}

function isInvalidSuite(suite) {
return (
(!suite.root && suite.title === '') ||
(suite.tests.length === 0 && suite.suites.length === 0)
);
}

/**
* Parses title for tags in format @tagName=value
* @param {} testTitle
Expand Down Expand Up @@ -103,7 +96,7 @@ function MochaXUnitReporter(runner, options) {
this._runner.on(
'suite',
function(suite) {
if (!isInvalidSuite(suite)) {
if (suite.tests.length) {
collections.push(this.getCollectionData(suite));
}
}.bind(this)
Expand Down Expand Up @@ -151,7 +144,7 @@ MochaXUnitReporter.prototype.getCollectionData = function(suite) {
collection: [
{
_attr: {
name: suite.title || 'Root Suite',
name: suite.title || 'Untitled Collection',
total: suite.tests.length
}
}
Expand Down Expand Up @@ -212,6 +205,28 @@ MochaXUnitReporter.prototype.getTestData = function(test, status) {
});
}

if (status === 'failed') {
testCase.test.push({
failure: [
{
_attr: {
'exception-type': test.err.name
}
},
{
message: {
_cdata: test.err.message
}
},
{
'stack-trace': {
_cdata: test.err.stack
}
}
]
});
}

return testCase;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mocha-xunit-reporter",
"version": "2.0.0",
"version": "2.1.0",
"description": "A Mocha xunit reporter",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions test/helpers/mock-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use strict';

function Test(fullTitle, title, duration) {
return {
function Test(fullTitle, title, duration, err) {
var test = {
title: title,
duration: duration,
fullTitle: function() { return fullTitle; },
slow: function() {}
};

if (err) {
test[err] = err;
}

return test;
}

module.exports = Test;
35 changes: 22 additions & 13 deletions test/mocha-xunit-reporter-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,30 @@ describe('mocha-xunit-reporter', () => {
}

runner.fail(
new Test('Bar can narfle the garthog', 'can narfle the garthog', 1),
new Test('Bar can narfle the garthog', 'can narfle the garthog', 1, {
name: 'BarError',
message: 'expected garthog to be dead',
stack: 'expected garthog to be dead'
}),
{
stack:
options.invalidChar +
'expected garthog to be dead' +
options.invalidChar
name: 'BarError',
message: 'expected garthog to be dead',
stack: 'expected garthog to be dead'
}
);

runner.fail(
new Test('Baz can behave like a flandip', 'can behave like a flandip', 1),
new Test('Baz can behave like a flandip', 'can behave like a flandip', 1, {
name: 'BazError',
message:
'expected baz to be masher, a hustler, an uninvited grasper of cone',
stack: 'BazFile line:1\nBazFile line:2'
}),
{
name: 'BazError',
message:
'expected baz to be masher, a hustler, an uninvited grasper of cone'
'expected baz to be masher, a hustler, an uninvited grasper of cone',
stack: 'BazFile line:1\nBazFile line:2'
}
);

Expand Down Expand Up @@ -234,8 +243,8 @@ describe('mocha-xunit-reporter', () => {
reporter = spyingReporter();
});

it('skips suites with empty title', function() {
runner.startSuite({ title: '', tests: [1] });
it('skips suites with empty tests', function() {
runner.startSuite({ title: '', tests: [] });
runner.end();

expect(assembly).to.be.empty;
Expand All @@ -249,7 +258,7 @@ describe('mocha-xunit-reporter', () => {
});

it('does not skip suites with nested suites', function() {
runner.startSuite({ title: 'test me', suites: [1] });
runner.startSuite({ title: 'test me', suites: [1], tests: [1] });
runner.end();

expect(assembly).to.have.length(1);
Expand All @@ -263,18 +272,18 @@ describe('mocha-xunit-reporter', () => {
});

it('does not skip root suite', function() {
runner.startSuite({ title: '', root: true, suites: [1] });
runner.startSuite({ title: '', root: true, suites: [1], tests: [1] });
runner.end();

expect(assembly).to.have.length(1);
});

it('uses "Root Suite" by default', function() {
runner.startSuite({ title: '', root: true, suites: [1] });
runner.startSuite({ title: '', root: true, suites: [1], tests: [1] });
runner.end();
expect(assembly[0].collection[0]._attr).to.have.property(
'name',
'Root Suite'
'Untitled Collection'
);
});

Expand Down
34 changes: 34 additions & 0 deletions test/mock-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ module.exports = function(stats, options) {
time: '0.001',
result: 'failed'
}
},
{
failure: [
{
_attr: {
'exception-type': 'BarError'
}
},
{
message: 'expected garthog to be dead'
},
{
'stack-trace': {
_cdata: 'expected garthog to be dead'
}
}
]
}
]
},
Expand All @@ -62,6 +79,23 @@ module.exports = function(stats, options) {
time: '0.001',
result: 'failed'
}
},
{
failure: [
{
_attr: {
'exception-type': 'BazError'
}
},
{
message: 'expected baz to be masher, a hustler, an uninvited grasper of cone'
},
{
'stack-trace': {
_cdata: 'BazFile line:1\nBazFile line:2'
}
}
]
}
]
}
Expand Down

0 comments on commit 9a5ae60

Please sign in to comment.