Back to Repositories

Validating Hebrew Locale Relative Time Formatting in dayjs

This test suite validates the Hebrew (he) locale implementation for relative time formatting in the dayjs library. It ensures proper translation and formatting of time differences in Hebrew, comparing the output with moment.js as the reference implementation.

Test Coverage Overview

The test suite provides comprehensive coverage of relative time formatting in Hebrew locale.

Key areas tested include:
  • Various time intervals from seconds to years
  • Different formatting variations (from, to, without suffix)
  • Boundary cases for time unit thresholds
  • Direct comparison with moment.js output

Implementation Analysis

The testing approach employs a data-driven methodology using Jest’s test framework. The implementation iterates through a predefined array of time intervals and units, comparing dayjs and moment.js outputs for each case.

Technical patterns include:
  • Plugin extension for relative time functionality
  • Locale switching for Hebrew language
  • Parallel testing against moment.js reference

Technical Details

Testing infrastructure includes:
  • Jest as the testing framework
  • dayjs relative time plugin integration
  • Hebrew locale implementation
  • moment.js for comparison validation
  • Array-driven test cases for systematic verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices in JavaScript locale testing.

Notable practices include:
  • Systematic test case organization
  • Reference implementation comparison
  • Comprehensive time unit coverage
  • Isolation of locale-specific functionality
  • Consistent assertion patterns

iamkun/dayjs

test/locale/he.test.js

            
import moment from 'moment'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/he'

dayjs.extend(relativeTime)

it('RelativeTime: Time from X', () => {
  const T = [
    [44.4, 'second'], // a few seconds
    [89.5, 'second'], // a minute
    [2, 'minute'], // 2 minutes
    [43, 'minute'], // 43 minutes
    [45, 'minute'], // an hour
    [3, 'hour'], // 3 hours
    [21, 'hour'], // 21 hours
    [1, 'day'], // a day
    [3, 'day'], // 3 day
    [25, 'day'], // 25 days
    [1, 'month'], // a month
    [2, 'month'], // 2 month
    [10, 'month'], // 10 month
    [1, 'year'], // a year
    [2, 'year'], // 2 year
    [5, 'year'], // 5 year
    [18, 'month'] // 2 years
  ]

  T.forEach((t) => {
    dayjs.locale('he')
    moment.locale('he')

    const dayjsDay = dayjs()
    const momentDay = moment()

    const dayjsCompare = dayjs().add(t[0], t[1])
    const momentCompare = moment().add(t[0], t[1])

    expect(dayjsDay.from(dayjsCompare)).toBe(momentDay.from(momentCompare))

    expect(dayjsDay.to(dayjsCompare)).toBe(momentDay.to(momentCompare))

    expect(dayjsDay.from(dayjsCompare, true)).toBe(momentDay.from(momentCompare, true))
  })
})