Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/cli/src/ui/utils/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ describe('formatters', () => {
expect(formatDuration(-100)).toBe('0s');
});

it('should roll a sub-minute value up to "1m" when it rounds to 60s', () => {
// 59.95s and up round to "60.0" at one decimal, which is not a valid
// sub-minute reading; it should render as the minute it rounds to,
// matching formatDuration(60000) === '1m'.
expect(formatDuration(59949)).toBe('59.9s');
expect(formatDuration(59950)).toBe('1m');
expect(formatDuration(59999)).toBe('1m');
expect(formatDuration(59950, { hideTrailingZeros: true })).toBe('1m');
});

describe('with hideTrailingZeros', () => {
it('drops .0 suffix for whole seconds under a minute', () => {
expect(formatDuration(5000, { hideTrailingZeros: true })).toBe('5s');
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/ui/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export const formatDuration = (

if (totalSeconds < 60) {
const formatted = totalSeconds.toFixed(1);
// toFixed can round up across the minute boundary (e.g. 59.95s -> "60.0"),
// which is not a valid sub-minute reading. Render it as the minute it
// rounds to, matching formatDuration(60000) === '1m'.
if (parseFloat(formatted) >= 60) {
return '1m';
}
if (options?.hideTrailingZeros && formatted.endsWith('.0')) {
return `${formatted.slice(0, -2)}s`;
}
Expand Down
Loading