Back to Repositories

Validating ISO Week Calculations in dayjs

This test suite validates the isoWeeksInYear plugin functionality in Day.js, ensuring accurate calculation of ISO weeks per year. The tests verify the correct number of ISO weeks for different years, particularly handling edge cases like leap years with 53 weeks.

Test Coverage Overview

The test suite provides comprehensive coverage of the isoWeeksInYear plugin, examining multiple years between 2004 and 2015. Key functionality includes:

  • Validation of years with 53 ISO weeks (2004, 2009, 2015)
  • Verification of standard 52-week years
  • Integration with isLeapYear plugin for complete date handling

Implementation Analysis

The testing approach utilizes Jest’s assertion framework with MockDate for consistent date handling. The implementation follows a systematic pattern of testing consecutive years to ensure reliable week calculations.

The tests leverage dayjs plugin extension system and Jest’s expect().toBe() matcher for precise assertions.

Technical Details

Testing infrastructure includes:

  • Jest testing framework
  • MockDate for date manipulation
  • Day.js core library
  • isoWeeksInYear and isLeapYear plugins
  • Beforeach/afterEach hooks for test isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation through MockDate reset, consistent assertion patterns, and comprehensive year coverage. The code organization maintains clarity with distinct setup and teardown phases, while the systematic year-by-year testing approach ensures thorough validation of the plugin’s functionality.

iamkun/dayjs

test/plugin/isoWeeksInYear.test.js

            
import MockDate from 'mockdate'
import dayjs from '../../src'
import isoWeeksInYear from '../../src/plugin/isoWeeksInYear'
import isLeapYear from '../../src/plugin/isLeapYear'

dayjs.extend(isoWeeksInYear)
dayjs.extend(isLeapYear)

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

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

it('isoWeeksInYear', () => {
  expect(dayjs('2004').isoWeeksInYear()).toBe(53)
  expect(dayjs('2005').isoWeeksInYear()).toBe(52)
  expect(dayjs('2006').isoWeeksInYear()).toBe(52)
  expect(dayjs('2007').isoWeeksInYear()).toBe(52)
  expect(dayjs('2008').isoWeeksInYear()).toBe(52)
  expect(dayjs('2009').isoWeeksInYear()).toBe(53)
  expect(dayjs('2010').isoWeeksInYear()).toBe(52)
  expect(dayjs('2011').isoWeeksInYear()).toBe(52)
  expect(dayjs('2012').isoWeeksInYear()).toBe(52)
  expect(dayjs('2013').isoWeeksInYear()).toBe(52)
  expect(dayjs('2014').isoWeeksInYear()).toBe(52)
  expect(dayjs('2015').isoWeeksInYear()).toBe(53)
})