Back to Repositories

Testing Core Utility Functions in dayjs

This test suite validates core utility functions in the Day.js library, focusing on string formatting, timezone handling, and padding operations. The tests ensure reliable date-time manipulations across different formats and timezones.

Test Coverage Overview

The test suite provides comprehensive coverage of three essential utility functions: prettyUnit for unit normalization, padZoneStr for timezone formatting, and padStart for number padding.

  • PrettyUnit tests validate date unit string normalization across various formats
  • PadZoneStr verifies timezone offset string formatting for different UTC offsets
  • PadStart confirms proper zero-padding functionality for numerical values

Implementation Analysis

The testing approach utilizes Jest’s expect assertions to verify function outputs against expected values. Each utility function is tested independently with multiple test cases covering standard scenarios and edge cases.

The implementation leverages Jest’s it() blocks for discrete test cases, maintaining clear separation of concerns between different utility functions.

Technical Details

Testing Framework: Jest
Test Type: Unit Tests

  • Utils module imported from ../src/utils
  • Function-level unit testing with isolated scopes
  • Direct function calls with various input parameters
  • Explicit expect() assertions for output validation

Best Practices Demonstrated

The test suite demonstrates several testing best practices including comprehensive input validation, clear test case organization, and thorough edge case coverage.

  • Consistent test case naming and organization
  • Comprehensive validation of function outputs
  • Coverage of both valid and edge case scenarios
  • Clear separation between different utility function tests

iamkun/dayjs

test/utils.test.js

            
import Utils from '../src/utils'

const prettyUnit = Utils.p
const padStart = Utils.s
const padZoneStr = Utils.z

it('PrettyUnit', () => {
  expect(prettyUnit('Days')).toBe('day')
  expect(prettyUnit('days')).toBe('day')
  expect(prettyUnit('day')).toBe('day')
  expect(prettyUnit('quarter')).toBe('quarter')
  expect(prettyUnit('quarters')).toBe('quarter')
  expect(prettyUnit('D')).toBe('date')
  expect(prettyUnit('d')).toBe('day')
  expect(prettyUnit('M')).toBe('month')
  expect(prettyUnit('y')).toBe('year')
  expect(prettyUnit('h')).toBe('hour')
  expect(prettyUnit('m')).toBe('minute')
  expect(prettyUnit('s')).toBe('second')
  expect(prettyUnit('ms')).toBe('millisecond')
  expect(prettyUnit('Q')).toBe('quarter')
  expect(prettyUnit()).toBe('')
})

it('PadZoneStr', () => {
  const instance = {}
  instance.utcOffset = () => 0 * -1
  expect(padZoneStr(instance)).toBe('+00:00')
  instance.utcOffset = () => 1 * 60 * -1
  expect(padZoneStr(instance)).toBe('-01:00')
  instance.utcOffset = () => -1 * 60 * -1
  expect(padZoneStr(instance)).toBe('+01:00')
  instance.utcOffset = () => -10 * 60 * -1
  expect(padZoneStr(instance)).toBe('+10:00')
  instance.utcOffset = () => 10 * 60 * -1
  expect(padZoneStr(instance)).toBe('-10:00')
  instance.utcOffset = () => ((-5 * 60) - 30) * -1
  expect(padZoneStr(instance)).toBe('+05:30')
})

it('PadStart', () => {
  expect(padStart(1, 2, '0')).toBe('01')
  expect(padStart(0, 2, '0')).toBe('00')
})