Back to Repositories

Testing Chinese Locale Meridiem Implementation in dayjs

This test suite validates the Chinese (zh-cn) locale implementation in the Day.js library, focusing on meridiem (AM/PM) formatting. It ensures correct time-of-day representations in Chinese characters across different time boundaries.

Test Coverage Overview

The test suite provides comprehensive coverage for Chinese locale meridiem formatting.

Key areas tested include:
  • Morning period (上午) before 11:00
  • Noon period (中午) between 11:00-12:59
  • Afternoon period (下午) after 13:00
  • Precise boundary transitions between time periods

Implementation Analysis

The testing approach employs Jest’s unit testing framework with MockDate for consistent date manipulation. The implementation uses specific time boundaries to verify locale-specific meridiem formatting, with particular attention to the Chinese noon period which differs from moment.js implementation.

The tests utilize dayjs().locale() method for switching to Chinese locale and format(‘A’) for meridiem output verification.

Technical Details

Testing tools and configuration:
  • Jest as the testing framework
  • MockDate for date mocking
  • Day.js core library
  • Chinese locale module import
  • beforeEach and afterEach hooks for test isolation

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test cases and proper test setup/teardown.

Notable practices include:
  • Consistent date mocking for reproducible tests
  • Clear boundary value testing
  • Explicit locale selection
  • Comprehensive edge case coverage

iamkun/dayjs

test/locale/zh-cn.test.js

            
// import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import '../../src/locale/zh-cn'

beforeEach(() => {
  MockDate.set(new Date())
})

afterEach(() => {
  MockDate.reset()
})

it('Meridiem', () => {
  // the '中午' is different to moment.js 11-13
  expect(dayjs('2020-01-01 10:59:59').locale('zh-cn').format('A')).toEqual('上午')
  expect(dayjs('2020-01-01 11:00:00').locale('zh-cn').format('A')).toEqual('中午')
  expect(dayjs('2020-01-01 12:59:59').locale('zh-cn').format('A')).toEqual('中午')
  expect(dayjs('2020-01-01 13:00:00').locale('zh-cn').format('A')).toEqual('下午')
})