diff --git a/index.js b/index.js index c680b84..b1a275d 100644 --- a/index.js +++ b/index.js @@ -441,7 +441,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) { diff --git a/test/mocha-junit-reporter-spec.js b/test/mocha-junit-reporter-spec.js index 6663582..d14a7ef 100644 --- a/test/mocha-junit-reporter-spec.js +++ b/test/mocha-junit-reporter-spec.js @@ -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'); @@ -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(); diff --git a/test/mock-junit-suites.js b/test/mock-junit-suites.js new file mode 100644 index 0000000..59cf218 --- /dev/null +++ b/test/mock-junit-suites.js @@ -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" + } + } + ] + }, + ] + }, + ]; + } +};