Back to Repositories

Validating Weekday Plugin Implementation in iamkun/dayjs

This test suite validates the weekday plugin functionality in Day.js by comparing its behavior with Moment.js across different locales and first-day-of-week configurations. The tests ensure consistent date handling and locale-specific weekday calculations.

Test Coverage Overview

The test suite provides comprehensive coverage of the weekday plugin functionality across multiple locales (English, Chinese, Arabic).

Key areas tested include:
  • Basic weekday calculations and manipulations
  • Locale-specific first day of week handling
  • Negative and positive weekday index operations
  • Date format consistency with Moment.js

Implementation Analysis

The testing approach employs Jest’s unit testing framework with direct comparisons to Moment.js as the reference implementation.

Testing patterns include:
  • Mock date management for consistent testing environment
  • Locale switching between test cases
  • Direct API comparison with Moment.js methods
  • Value and format verification across different date manipulations

Technical Details

Testing infrastructure includes:
  • Jest as the testing framework
  • MockDate for date manipulation
  • Day.js with weekday plugin
  • Moment.js for comparison
  • Multiple locale configurations (en, zh-cn, ar)

Best Practices Demonstrated

The test suite exemplifies several testing best practices in JavaScript date handling.

Notable practices include:
  • Proper test isolation with beforeEach/afterEach hooks
  • Consistent state reset between tests
  • Comprehensive edge case coverage
  • Cross-library validation
  • Locale-aware testing methodology

iamkun/dayjs

test/plugin/weekday.test.js

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

dayjs.extend(weekday)

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

afterEach(() => {
  MockDate.reset()
  moment.locale('en')
  dayjs.locale('en')
})

it('Sunday is the first day of the week', () => {
  expect(dayjs().weekday()).toBe(moment().weekday())
  expect(dayjs().weekday(0).date()).toBe(moment().weekday(0).date())
  expect(dayjs().weekday(-7).format()).toBe(moment().weekday(-7).format())
  expect(dayjs().weekday(7).format()).toBe(moment().weekday(7).format())
})

it('Monday is the first day of the week', () => {
  moment.locale('zh-cn')
  dayjs.locale('zh-cn')
  expect(dayjs().weekday()).toBe(moment().weekday())
  expect(dayjs().weekday(0).date()).toBe(moment().weekday(0).date())
  expect(dayjs().weekday(-7).format()).toBe(moment().weekday(-7).format())
  expect(dayjs().weekday(7).format()).toBe(moment().weekday(7).format())
  const d1 = '2020-01-05'
  expect(dayjs(d1).weekday()).toBe(moment(d1).weekday())
  const d2 = '2020-01-07'
  expect(dayjs(d2).weekday()).toBe(moment(d2).weekday())
})

it('Saturday is the first day of the week', () => {
  moment.locale('ar')
  dayjs.locale('ar')
  expect(dayjs().weekday()).toBe(moment().weekday())
  expect(dayjs().weekday(0).date()).toBe(moment().weekday(0).date())
  expect(dayjs().weekday(-7).valueOf()).toBe(moment().weekday(-7).valueOf())
  expect(dayjs().weekday(7).valueOf()).toBe(moment().weekday(7).valueOf())
})