michaelleeallen / mocha-junit-reporter

A JUnit XML reporter for mocha.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

testsuite time does not include setup or teardown

gabegorelick opened this issue · comments

It looks like testsuite (and testsuites) is calculating the time attribute by summing the time each testcase took. However, this ignores the amount of time that setup and teardown take.

Example test:

describe('suite', function () {
  before(done => setTimeout(done, 1000));

  it('should complete immediately', () => {});
});

Mocha's spec and xunit reporters correctly output that this suite took 1s. Here's the xunit output for example:

<testsuite name="Mocha Tests" tests="1" failures="0" errors="0" skipped="0" timestamp="Fri, 08 May 2020 17:33:39 GMT" time="1.008">
<testcase classname="suite" name="should complete immediately" time="0"/>
</testsuite>

However, mocha-junit-reporter incorrectly reports that the entire testsuite took 0s:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Mocha Tests" time="0.0000" tests="1" failures="0">
  <testsuite name="Root Suite" timestamp="2020-05-08T17:30:12" tests="0" time="0.0000" failures="0">
  </testsuite>
  <testsuite name="suite" timestamp="2020-05-08T17:30:12" tests="1" file="test.js" time="0.0000" failures="0">
    <testcase name="suite should complete immediately" time="0.0000" classname="example test">
    </testcase>
  </testsuite>
</testsuites>

Relevant code:

var suiteTime = 0;
_cases.forEach(function(testcase) {
var lastNode = testcase.testcase[testcase.testcase.length - 1];
_suiteAttr.skipped += Number('skipped' in lastNode);
_suiteAttr.failures += Number('failure' in lastNode);
suiteTime += testcase.testcase[0]._attr.time;
testcase.testcase[0]._attr.time = testcase.testcase[0]._attr.time.toFixed(4);
});
_suiteAttr.time = suiteTime.toFixed(4);