Back to Repositories

Testing Buddhist Era Date Formatting in iamkun/dayjs

This test suite validates the Buddhist Era plugin functionality in dayjs, ensuring accurate date formatting according to the Buddhist calendar system. The tests verify both 2-digit and 4-digit year conversions, along with proper integration with other date formatting options.

Test Coverage Overview

The test suite provides comprehensive coverage of Buddhist Era date formatting functionality.

Key areas tested include:
  • Basic date formatting compatibility with moment.js
  • 2-digit Buddhist year conversion (BB format)
  • 4-digit Buddhist year conversion (BBBB format)
  • Combined formatting with other date elements
  • Proper handling of escaped format strings

Implementation Analysis

The testing approach utilizes Jest’s assertion framework with MockDate for consistent date handling. The implementation follows a systematic pattern of comparing dayjs output with moment.js reference implementations, ensuring cross-library compatibility.

The tests leverage Jest’s it() blocks for individual test cases and beforeEach/afterEach hooks for test isolation.

Technical Details

Testing tools and setup:
  • Jest as the testing framework
  • MockDate for date manipulation
  • moment.js for reference implementations
  • dayjs Buddhist Era plugin integration
  • beforeEach and afterEach hooks for test state management

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable elements include:
  • Isolated test cases with clear assertions
  • Consistent test setup and teardown
  • Comprehensive edge case coverage
  • Cross-library validation
  • Clear test case descriptions
  • Proper plugin extension handling

iamkun/dayjs

test/plugin/buddhistEra.test.js

            
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import buddhistEra from '../../src/plugin/buddhistEra'

dayjs.extend(buddhistEra)

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

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

it('Format empty string', () => {
  expect(dayjs().format()).toBe(moment().format())
})

it('Format Buddhist Era 2 digit', () => {
  expect(dayjs().format('BB')).toBe(`${(moment().year() + 543) % 100}`)
})

it('Format Buddhist Era 4 digit', () => {
  expect(dayjs().format('BBBB')).toBe(`${moment().year() + 543}`)
})

it('Format Buddhist Era 4 digit with other format', () => {
  const format = 'D MMM BBBB'
  const today = moment()
  const momentDate = today.format(format).replace('BBBB', today.year() + 543)
  expect(dayjs().format(format)).toBe(momentDate)
})

it('Skips format strings inside brackets', () => {
  expect(dayjs().format('[BBBB]')).toBe('BBBB')
  expect(dayjs().format('[BB]')).toBe('BB')
})