Back to Repositories

Testing BigInt Timestamp Support in Day.js Library

This test suite validates the BigInt support plugin functionality in Day.js, focusing on timestamp parsing capabilities. The tests ensure proper handling of both regular and BigInt timestamps, comparing results with Moment.js for consistency.

Test Coverage Overview

The test suite provides comprehensive coverage of BigInt timestamp parsing functionality.

Key areas tested include:
  • Parsing millisecond timestamps as BigInt
  • Unix timestamp handling with BigInt values
  • Compatibility with standard number timestamps
  • Cross-validation with Moment.js results

Implementation Analysis

The testing approach employs Jest’s unit testing framework with MockDate for consistent timestamp testing. The implementation uses direct comparison testing patterns, verifying timestamp parsing and conversion accuracy through valueOf() method calls.

Technical patterns include:
  • Mock date management with setup/teardown
  • Direct value comparisons between Day.js and Moment.js
  • BigInt conversion testing

Technical Details

Testing infrastructure includes:
  • Jest as the testing framework
  • MockDate for date manipulation
  • BigInt native JavaScript support
  • Moment.js for comparison validation
Configuration includes beforeEach and afterEach hooks for test isolation.

Best Practices Demonstrated

The test suite exemplifies several testing best practices for timestamp handling.

Notable practices include:
  • Proper test isolation using MockDate
  • Comprehensive edge case coverage
  • Cross-library validation
  • Clear test case organization
  • Explicit value comparisons

iamkun/dayjs

test/plugin/bigIntSupport.test.js

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

dayjs.extend(bigIntSupport)

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

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

/* global BigInt */

it('Parse BigInt ts and tsms', () => {
  const tsms = 1666310421101
  const tsmsBig = BigInt(tsms)
  const ts = 1666311003
  const tsBig = BigInt(ts)
  const momentTsms = moment(tsms)
  const momentTs = moment.unix(ts)
  expect(dayjs(tsms).valueOf()).toBe(momentTsms.valueOf())
  expect(dayjs(tsms).valueOf()).toBe(dayjs(tsmsBig).valueOf())
  expect(dayjs.unix(ts).valueOf()).toBe(momentTs.valueOf())
  expect(dayjs.unix(tsBig).valueOf()).toBe(dayjs.unix(tsBig).valueOf())
})