Back to Repositories

Validating Array Support Date Parsing in Day.js

This test suite validates the array support plugin functionality in Day.js by comparing its behavior with Moment.js. It focuses on parsing date arrays in both local and UTC formats, ensuring consistent date handling across different input patterns.

Test Coverage Overview

The test suite provides comprehensive coverage for array-based date parsing functionality.

Key areas tested include:
  • Empty array handling in both local and UTC modes
  • Various array formats with different date components
  • Direct comparison with Moment.js output
  • Multiple date component combinations from year-only to millisecond precision

Implementation Analysis

The testing approach employs Jest’s describe/it pattern for structured test organization. Each test case uses direct comparison between Day.js and Moment.js output to ensure compatibility.

Notable patterns include:
  • MockDate usage for consistent date testing
  • Array iteration testing with forEach
  • Plugin extension verification
  • Parallel local and UTC testing structures

Technical Details

Testing infrastructure includes:
  • Jest as the testing framework
  • MockDate for date manipulation
  • Day.js plugin system integration
  • Moment.js for comparison validation
  • UTC plugin for timezone handling
  • Array support plugin for date array parsing

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable examples include:
  • Consistent test setup and teardown with beforeEach/afterEach
  • Systematic test case organization
  • Comprehensive edge case coverage
  • Clear test descriptions
  • Efficient test case reuse through arrays
  • Direct feature parity testing with reference implementation

iamkun/dayjs

test/plugin/arraySupport.test.js

            
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import arraySupport from '../../src/plugin/arraySupport'
import utc from '../../src/plugin/utc'

dayjs.extend(utc)
dayjs.extend(arraySupport)

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

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

describe('parse empty array', () => {
  it('local', () => {
    expect(dayjs([]).format())
      .toBe(moment([]).format())
  })
  it('utc', () => {
    expect(dayjs.utc([]).format())
      .toBe(moment.utc([]).format())
  })
})

const testArrs = [
  [2010, 1, 14, 15, 25, 50, 125],
  [2010],
  [2010, 6],
  [2010, 6, 10]
]

describe('parse array local', () => {
  testArrs.forEach((testArr) => {
    it(testArr, () => {
      expect(dayjs(testArr).format())
        .toBe(moment(testArr).format())
    })
  })
})

describe('parse array utc', () => {
  testArrs.forEach((testArr) => {
    it(testArr, () => {
      expect(dayjs.utc(testArr).format())
        .toBe(moment.utc(testArr).format())
    })
  })
})