Skip to content

Commit

Permalink
Fix bug when truncating testcase.attr.time (#172)
Browse files Browse the repository at this point in the history
Fixes #171.
Co-authored-by: Clay Jensen-Reimann <clayreimann@gmail.com>
  • Loading branch information
halfninja committed Sep 27, 2022
1 parent 5c240ae commit 4c2bc93
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ MochaJUnitReporter.prototype.getXml = function(testsuites) {

_suiteAttr.skipped += Number('skipped' in lastNode);
_suiteAttr.failures += Number('failure' in lastNode);
testcase.testcase[0]._attr.time = testcase.testcase[0]._attr.time.toFixed(3);
if (typeof testcase.testcase[0]._attr.time === 'number') {
testcase.testcase[0]._attr.time = testcase.testcase[0]._attr.time.toFixed(3);
}
});

if (antMode) {
Expand Down
7 changes: 7 additions & 0 deletions test/mocha-junit-reporter-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var FakeTimer = require('@sinonjs/fake-timers');
var xmllint = require('xmllint');
var chaiXML = require('chai-xml');
var mockXml = require('./mock-results');
var mockJunitSuites = require('./mock-junit-suites');
var testConsole = require('test-console');

var debug = require('debug')('mocha-junit-reporter:tests');
Expand Down Expand Up @@ -232,6 +233,12 @@ describe('mocha-junit-reporter', function() {
});
});

it('can handle getXml being called twice', function() {
var reporter = createReporter({mochaFile: 'test/output/mocha.xml'});
var testsuites = mockJunitSuites.withStringTimes();
reporter.getXml(testsuites);
});

it('respects `process.env.MOCHA_FILE`', function(done) {
process.env.MOCHA_FILE = 'test/output/results.xml';
var reporter = createReporter();
Expand Down
41 changes: 41 additions & 0 deletions test/mock-junit-suites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

module.exports = {
withStringTimes: function() {
return [
{
testsuite: [
{
_attr: {
name: "Foo Bar",
timestamp: "1970-01-01T00:00:00",
tests: "3",
failures: "2",
time: "100.001"
}
},
{
testcase: [
{
_attr: {
name: "Foo Bar can narfle the garthog",
classname: "can narfle the garthog",
time: "2.002"
}
},
{
failure: {
_attr: {
message: "expected garthog to be dead",
type: "Error"
},
_cdata: "this is where the stack would be"
}
}
]
},
]
},
];
}
};

0 comments on commit 4c2bc93

Please sign in to comment.