From 356fe4772f38fb3eccfef59f5952958093133a9e Mon Sep 17 00:00:00 2001 From: nieyuyao Date: Fri, 12 Aug 2022 16:51:05 +0800 Subject: [PATCH 1/2] fix: set the default duration to zero and remove trailing zeros --- packages/vitest/src/node/reporters/junit.ts | 5 +- .../__snapshots__/reporters.spec.ts.snap | 84 +++++++++---------- test/reporters/tests/junit.test.ts | 35 ++++++++ 3 files changed, 80 insertions(+), 44 deletions(-) create mode 100644 test/reporters/tests/junit.test.ts diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index ce599fc5051f..21d5398377b6 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).toFixed(10).replace(/^(\d+)((?:\.0+)|(\.\d+?)0+)$/, '$1$3') } 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') +}) + From 645afb48356d559ab9fab0edd621b2d871e18054 Mon Sep 17 00:00:00 2001 From: nieyuyao Date: Fri, 12 Aug 2022 21:52:00 +0800 Subject: [PATCH 2/2] fix: replace RegExp with toLocaleString --- packages/vitest/src/node/reporters/junit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index 21d5398377b6..47090179d245 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -60,7 +60,7 @@ function escapeXML(value: any): string { export function getDuration(task: Task): string | undefined { const duration = task.result?.duration ?? 0 - return (duration / 1000).toFixed(10).replace(/^(\d+)((?:\.0+)|(\.\d+?)0+)$/, '$1$3') + return (duration / 1000).toLocaleString(undefined, { useGrouping: false, maximumFractionDigits: 10 }) } export class JUnitReporter implements Reporter {