Back to Repositories

Testing Millisecond Time Formatting Implementation in OpenHands

This test suite validates the formatMs utility function that converts milliseconds into a formatted time string. The tests verify proper time formatting across different durations, ensuring accurate conversion of milliseconds to minutes and seconds format.

Test Coverage Overview

The test suite provides comprehensive coverage of the formatMs utility function.

Key functionality tested includes:
  • Basic second conversion (1000ms to ’00:01′)
  • Full minute conversion (60000ms to ’01:00′)
  • Fractional minute handling (150000ms to ’02:30′)
  • Larger time values (720000ms to ’12:00′)

Implementation Analysis

The testing approach utilizes Jest’s test and expect functions to validate time formatting logic. The implementation follows a clear pattern of testing increasingly complex time conversions, from simple seconds to multiple minutes.

The tests leverage TypeScript type safety and Jest’s toBe matcher for precise equality assertions.

Technical Details

Testing tools and setup:
  • Vitest as the testing framework
  • TypeScript for type-safe testing
  • Jest’s expect assertions
  • Individual test cases for different time scenarios

Best Practices Demonstrated

The test suite exemplifies several testing best practices in its implementation.

Notable practices include:
  • Clear test case organization
  • Progressive complexity in test scenarios
  • Consistent assertion patterns
  • Comprehensive edge case coverage
  • Focused, single-responsibility test cases

all-hands-ai/openhands

frontend/__tests__/utils/format-ms.test.ts

            
import { test, expect } from "vitest";
import { formatMs } from "../../src/utils/format-ms";

test("formatMs", () => {
  expect(formatMs(1000)).toBe("00:01");
  expect(formatMs(1000 * 60)).toBe("01:00");
  expect(formatMs(1000 * 60 * 2.5)).toBe("02:30");
  expect(formatMs(1000 * 60 * 12)).toBe("12:00");
});