Skip to content

Commit

Permalink
fix: prefer ISO date format (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel authored Nov 10, 2023
1 parent 282c1f2 commit f4a5e25
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/reporters/junitReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ApexTestResultOutcome,
TestResult
} from '../tests/types';
import { msToSecond } from '../utils';
import { formatStartTime, msToSecond } from '../utils';

// cli currently has spaces in multiples of four for junit format
const tab = ' ';
Expand All @@ -29,7 +29,7 @@ export class JUnitReporter {
let output = `<?xml version="1.0" encoding="UTF-8"?>\n`;
output += `<testsuites>\n`;
output += `${tab}<testsuite name="force.apex" `;
output += `timestamp="${new Date(summary.testStartTime).toISOString()}" `;
output += `timestamp="${summary.testStartTime}" `;
output += `hostname="${summary.hostname}" `;
output += `tests="${summary.testsRan}" `;
output += `failures="${summary.failing}" `;
Expand Down Expand Up @@ -61,6 +61,10 @@ export class JUnitReporter {
value = 'Successful';
}

if (key === 'testStartTime') {
value = formatStartTime(value);
}

junitProperties += `${tab}${tab}${tab}<property name="${key}" value="${value}"/>\n`;
});

Expand Down
2 changes: 1 addition & 1 deletion src/tests/asyncTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export class AsyncTests {
passRate: calculatePercentage(globalTests.passed, testResults.length),
failRate: calculatePercentage(globalTests.failed, testResults.length),
skipRate: calculatePercentage(globalTests.skipped, testResults.length),
testStartTime: formatStartTime(testRunSummary.StartTime),
testStartTime: formatStartTime(testRunSummary.StartTime, 'ISO'),
testExecutionTimeInMs: testRunSummary.TestTime ?? 0,
testTotalTimeInMs: testRunSummary.TestTime ?? 0,
commandTimeInMs: getCurrentTime() - commandStartTime,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/syncTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class SyncTests {
apiTestResult.numTestsRun
),
skipRate: calculatePercentage(0, apiTestResult.numTestsRun),
testStartTime: formatStartTime(startTime),
testStartTime: formatStartTime(startTime, 'ISO'),
testExecutionTimeInMs: apiTestResult.totalTime ?? 0,
testTotalTimeInMs: apiTestResult.totalTime ?? 0,
commandTimeInMs: getCurrentTime() - startTime,
Expand Down
12 changes: 10 additions & 2 deletions src/utils/dateUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ export function getCurrentTime(): number {
/**
* Returns the formatted date and time given the milliseconds in numbers or UTC formatted string
* @param startTime start time in millisecond numbers or UTC format string
* @returns date and time formatted for locale
* @param format either 'ISO' or 'locale'. Defaults to 'locale' to keep backward compatible.
* @returns formatted date and time
*/
export function formatStartTime(startTime: string | number): string {
export function formatStartTime(
startTime: string | number,
format: 'ISO' | 'locale' = 'locale'
): string {
const date = new Date(startTime);
if (format === 'ISO') {
return date.toISOString();
}

return `${date.toDateString()} ${date.toLocaleTimeString()}`;
}

Expand Down
7 changes: 4 additions & 3 deletions test/reporters/testResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ApexTestResultOutcome, TestResult } from '../../src/tests/types';

const testStartTime = '2020-11-09T18:02:50.000+0000';
const date = new Date(testStartTime);
const isoStartTime = date.toISOString();
const localStartTime = `${date.toDateString()} ${date.toLocaleTimeString()}`;

export const coverageResult: TestResult = {
Expand All @@ -20,7 +21,7 @@ export const coverageResult: TestResult = {
outcome: 'Completed',
passRate: '100%',
skipRate: '0%',
testStartTime: localStartTime,
testStartTime: isoStartTime,
testExecutionTimeInMs: 5463,
testTotalTimeInMs: 5463,
commandTimeInMs: 6000,
Expand Down Expand Up @@ -134,7 +135,7 @@ export const successResult: TestResult = {
outcome: 'Completed',
passRate: '100%',
skipRate: '0%',
testStartTime: localStartTime,
testStartTime: isoStartTime,
testExecutionTimeInMs: 5463,
testTotalTimeInMs: 5463,
commandTimeInMs: 6000,
Expand Down Expand Up @@ -196,7 +197,7 @@ export const testResults: TestResult = {
outcome: 'Completed',
passRate: '88%',
skipRate: '0%',
testStartTime: localStartTime,
testStartTime: isoStartTime,
testExecutionTimeInMs: 5463,
testTotalTimeInMs: 5463,
commandTimeInMs: 6000,
Expand Down
3 changes: 1 addition & 2 deletions test/tests/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ export const syncTestResultWithFailures: SyncTestResult = {
};

export const testStartTime = '2020-11-09T18:02:50.000+0000';
const date = new Date(testStartTime);
const localStartTime = `${date.toDateString()} ${date.toLocaleTimeString()}`;
const localStartTime = new Date(testStartTime).toISOString();
export const testRunId = '707xx0000AGQ3jbQQD';

export const syncResult: TestResult = {
Expand Down
31 changes: 31 additions & 0 deletions test/utils/dateUtil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { expect } from 'chai';
import * as dateUtil from '../../src/utils/dateUtil';

describe('Date Utils', () => {
const testStartTime = '2020-11-09T18:02:50.000+0000';
const testStartTimeDate = new Date(testStartTime);

it('should format a date to locale by default', () => {
const expectedFormat = `${testStartTimeDate.toDateString()} ${testStartTimeDate.toLocaleTimeString()}`;
expect(dateUtil.formatStartTime(testStartTime)).to.equal(expectedFormat);
});

it('should format a date to locale by param', () => {
const expectedFormat = `${testStartTimeDate.toDateString()} ${testStartTimeDate.toLocaleTimeString()}`;
expect(dateUtil.formatStartTime(testStartTime, 'locale')).to.equal(
expectedFormat
);
});

it('should format a date to ISO', () => {
expect(dateUtil.formatStartTime(testStartTime, 'ISO')).to.equal(
testStartTimeDate.toISOString()
);
});
});

0 comments on commit f4a5e25

Please sign in to comment.