[Synthetics] Improve test duration formatting (#152151)

Co-authored-by: Shahzad <shahzad31comp@gmail.com>
This commit is contained in:
Justin Kambic 2023-02-28 05:02:12 -05:00 committed by GitHub
parent ee56d954da
commit f688b37c89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View file

@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { formatTestDuration } from './test_time_formats';
describe('formatTestDuration', () => {
it.each`
duration | expected | isMilli
${undefined} | ${'0 ms'} | ${undefined}
${120_000_000} | ${'2 min'} | ${undefined}
${6_200_000} | ${'6.2 s'} | ${false}
${500_000} | ${'500 ms'} | ${undefined}
${100} | ${'0 ms'} | ${undefined}
${undefined} | ${'0 ms'} | ${true}
${600_000} | ${'10 min'} | ${true}
${6_200} | ${'6.2 s'} | ${true}
${500} | ${'500 ms'} | ${true}
`(
'returns $expected when `duration` is $duration and `isMilli` $isMilli',
({
duration,
expected,
isMilli,
}: {
duration?: number;
expected: string;
isMilli?: boolean;
}) => {
expect(formatTestDuration(duration, isMilli)).toBe(expected);
}
);
});

View file

@ -17,11 +17,11 @@ export const formatTestDuration = (duration = 0, isMilli = false) => {
const secs = isMilli ? duration / 1e3 : duration / 1e6;
if (secs >= 60) {
return `${(secs / 60).toFixed(1)} min`;
return `${parseFloat((secs / 60).toFixed(1))} min`;
}
if (secs >= 1) {
return `${secs.toFixed(1)} s`;
return `${parseFloat(secs.toFixed(1))} s`;
}
if (isMilli) {