vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Keep time attribute in JUnit XML file

Open linbudu599 opened this issue 2 years ago • 0 comments

Describe the bug

Currently in junit reporter, when task.result.duration's value is 0, the duration got undefined as fallback value by JavaScript's implicit conversion, which results in that generated report file will not got time attribute in <testcase> tag.

function getDuration(task: Task): string | undefined {
  return task.result?.duration ? (task.result.duration / 1000).toFixed(10) : undefined
}

The produced xml file:

<testsuites id="" name="" tests="1" failures="0" skipped="0" errors="0" time="0.014961999997496605">
  <testsuite name="x" timestamp="2022-08-09T09:17:24.527Z" hostname="" tests="5" failures="0" errors="0" skipped="0" time="0.0070000000">
    <!-- no time attribute was present -->
    <testcase classname="x" name="should xxx">
    </testcase>
  </testsuite>
</testsuites>

In junit standard, time attribute should be kept even using value '0'. Also, this will cause some tools work incorrectly.

Use 0 as fallback value should fix this:

function getDuration(task: Task): string {
  return typeof task.result?.duration === 'number' ? task.result.duration === 0 ? '0' : (task.result.duration / 1000).toFixed(10) : '0'
}

Reproduction

I'm not sure why the task.result.duration reveived value 0, but in some simple test cases(e.g. only one expect statement) it always appear, so I suspect that this is also a precision loss problem caused by JavaScript.

System Info

System:
    OS: macOS 12.3.1
    CPU: (8) arm64 Apple M1
    Memory: 90.95 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.4 - ~/.nvm/versions/node/v14.17.4/bin/node
    Yarn: 1.22.18 - ~/.yarn/bin/yarn
    npm: 8.10.0 - ~/.nvm/versions/node/v14.17.4/bin/npm
  Browsers:
    Chrome: 104.0.5112.79
    Edge: 104.0.1293.47
    Firefox: 89.0.2
    Safari: 15.4

Used Package Manager

npm

Validations

linbudu599 avatar Aug 09 '22 11:08 linbudu599