Back to Repositories

Testing Time Formatting Utility Implementation in Parcel Bundler

This test suite validates the prettifyTime utility function that formats time durations into human-readable strings. The tests ensure accurate conversion of milliseconds to appropriate time formats with proper units and decimal precision.

Test Coverage Overview

The test coverage focuses on two main scenarios for time formatting:
  • Millisecond formatting for values under 1000
  • Second formatting with decimal precision for values over 1000
Edge cases include zero values and precise decimal handling for larger numbers. Integration points include the core utility function’s interaction with the time formatting system.

Implementation Analysis

The testing approach uses Jest’s describe/it pattern for organizing test cases. The implementation leverages Node’s assert library for equality checking, demonstrating a straightforward unit testing pattern. Each test case validates specific formatting rules with multiple assertions to ensure comprehensive coverage.

Technical Details

Testing tools and configuration:
  • Jest test runner
  • Flow type checking
  • Node.js assert library
  • Unit test isolation pattern

Best Practices Demonstrated

The test suite exhibits several testing best practices:
  • Clear test case organization using describe blocks
  • Multiple assertions per logical group
  • Explicit test descriptions
  • Boundary value testing
  • Consistent assertion patterns

parcel-bundler/parcel

packages/core/utils/test/prettifyTime.test.js

            
// @flow
import assert from 'assert';
import prettifyTime from '../src/prettifyTime';

describe('prettifyTime', () => {
  it('should format numbers less than 1000 as ms', () => {
    assert.equal(prettifyTime(888), '888ms');
    assert.equal(prettifyTime(50), '50ms');
    assert.equal(prettifyTime(0), '0ms');
  });

  it('should format numbers greater than 1000 as s with 2 fractional digits', () => {
    assert.equal(prettifyTime(4000), '4.00s');
    assert.equal(prettifyTime(90000), '90.00s');
    assert.equal(prettifyTime(45678), '45.68s');
  });
});