diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index ce599fc5051f..47090179d245 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -58,8 +58,9 @@ function escapeXML(value: any): string { true) } -function getDuration(task: Task): string | undefined { - return task.result?.duration ? (task.result.duration / 1000).toFixed(10) : undefined +export function getDuration(task: Task): string | undefined { + const duration = task.result?.duration ?? 0 + return (duration / 1000).toLocaleString(undefined, { useGrouping: false, maximumFractionDigits: 10 }) } export class JUnitReporter implements Reporter { diff --git a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap index a77a2789b5da..bd60ace995a4 100644 --- a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap +++ b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap @@ -3,32 +3,32 @@ exports[`JUnit reporter (no outputFile entry) 1`] = ` " - - + + AssertionError: expected 2.23606797749979 to equal 2 ❯ vitest/test/core/test/basic.test.ts:8:32 - + - + - + - + [33merror[39m - + @@ -39,32 +39,32 @@ AssertionError: expected 2.23606797749979 to equal 2 exports[`JUnit reporter 1`] = ` " - - + + AssertionError: expected 2.23606797749979 to equal 2 ❯ vitest/test/core/test/basic.test.ts:8:32 - + - + - + - + [33merror[39m - + @@ -80,32 +80,32 @@ exports[`JUnit reporter with outputFile 1`] = ` exports[`JUnit reporter with outputFile 2`] = ` " - - + + AssertionError: expected 2.23606797749979 to equal 2 ❯ vitest/test/core/test/basic.test.ts:8:32 - + - + - + - + [33merror[39m - + @@ -121,32 +121,32 @@ exports[`JUnit reporter with outputFile in non-existing directory 1`] = ` exports[`JUnit reporter with outputFile in non-existing directory 2`] = ` " - - + + AssertionError: expected 2.23606797749979 to equal 2 ❯ vitest/test/core/test/basic.test.ts:8:32 - + - + - + - + [33merror[39m - + @@ -162,32 +162,32 @@ exports[`JUnit reporter with outputFile object 1`] = ` exports[`JUnit reporter with outputFile object 2`] = ` " - - + + AssertionError: expected 2.23606797749979 to equal 2 ❯ vitest/test/core/test/basic.test.ts:8:32 - + - + - + - + [33merror[39m - + @@ -203,32 +203,32 @@ exports[`JUnit reporter with outputFile object in non-existing directory 1`] = ` exports[`JUnit reporter with outputFile object in non-existing directory 2`] = ` " - - + + AssertionError: expected 2.23606797749979 to equal 2 ❯ vitest/test/core/test/basic.test.ts:8:32 - + - + - + - + [33merror[39m - + diff --git a/test/reporters/tests/junit.test.ts b/test/reporters/tests/junit.test.ts new file mode 100644 index 000000000000..cdecccf75b10 --- /dev/null +++ b/test/reporters/tests/junit.test.ts @@ -0,0 +1,35 @@ +import type { Suite, Task, TaskResult } from 'vitest' +import { expect, test } from 'vitest' +import { getDuration } from '../../../packages/vitest/src/node/reporters/junit' + +test('calc the duration used by junit', () => { + const result: TaskResult = { state: 'pass', duration: 0 } + const suite: Suite = { + id: '1', + type: 'suite', + name: 'suite', + mode: 'run', + tasks: [], + } + const task: Task = { + id: '1', + type: 'test', + name: 'timeout', + mode: 'run', + result, + context: null as any, + suite, + } + expect(getDuration(task)).toBe('0') + result.duration = 0.12 + expect(getDuration(task)).toBe('0.00012') + result.duration = 12 + expect(getDuration(task)).toBe('0.012') + result.duration = 12.01 + expect(getDuration(task)).toBe('0.01201') + result.duration = 12000 + expect(getDuration(task)).toBe('12') + result.duration = 12001 + expect(getDuration(task)).toBe('12.001') +}) +