Back to Repositories

Testing Week of Year Calculations Implementation in dayjs

This test suite validates the Week of Year plugin functionality in Day.js by comparing its output with Moment.js. It ensures accurate week number calculations across different locales and edge cases throughout the year.

Test Coverage Overview

The test suite provides comprehensive coverage of the weekOfYear plugin functionality.

Key areas tested include:
  • Basic week number calculations
  • Week setting and getting operations
  • Locale-specific week calculations (en, en-gb)
  • Edge cases around year boundaries
  • Week number formatting options (w, ww, wo)

Implementation Analysis

The testing approach uses Jest’s assertion framework to compare Day.js outputs with Moment.js as the reference implementation.

Notable patterns include:
  • MockDate usage for consistent date handling
  • Direct comparison with Moment.js methods
  • Parameterized testing for edge cases
  • Plugin extension verification

Technical Details

Testing infrastructure includes:
  • Jest as the testing framework
  • MockDate for date manipulation
  • Multiple plugin extensions (weekOfYear, advancedFormat)
  • Locale configurations (en, en-gb)
  • Before/After hooks for test isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable examples include:
  • Isolated test cases with proper setup/teardown
  • Comprehensive edge case coverage
  • Consistent test structure and naming
  • Effective use of test case parameterization
  • Clear separation of locale-specific tests

iamkun/dayjs

test/plugin/weekOfYear.test.js

            
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import weekOfYear from '../../src/plugin/weekOfYear'
import advancedFormat from '../../src/plugin/advancedFormat'
import '../../src/locale/en-gb'

dayjs.extend(advancedFormat)
dayjs.extend(weekOfYear)

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

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

it('Week of year', () => {
  dayjs.locale('en')

  const day = '2018-12-31T10:59:09+08:00'
  const week = 27
  expect(dayjs(day).week()).toBe(moment(day).week())
  expect(dayjs().week()).toBe(moment().week())
  expect(dayjs().week(week).week()).toBe(moment().week(week).week())
  expect(dayjs().weeks(week).week()).toBe(moment().weeks(week).week())
  expect(dayjs().weeks(-week).week()).toBe(moment().weeks(-week).week())
  expect(dayjs().weeks(55).week()).toBe(moment().weeks(55).week())
  expect(dayjs().weeks()).toBe(moment().weeks())
})

it('Week of year with locale', () => {
  dayjs.locale('en-gb')
  moment.locale('en-gb')
  const day = '2019-07-28'
  expect(dayjs(day).week()).toBe(moment(day).week())
})

describe('Week of year with locale edges', () => {
  const testCases = [
    '2018-12-30',
    '2018-12-31',
    '2019-12-29',
    '2019-12-30',
    '2016-01-01',
    '2016-01-04'
  ]
  testCases.forEach((t) => {
    it(`Edges ${t}`, () => {
      expect(dayjs(t).week())
        .toBe(moment(t).week())
    })
  })
})

it('Format w ww wo', () => {
  const day = '2019-07-28'
  const D = dayjs(day)
  const M = moment(day)
  expect(D.format('w ww wo')).toBe(M.format('w ww wo'))
})