Back to Repositories

Testing isYesterday Date Validation in dayjs

This test suite validates the isYesterday plugin functionality in Day.js, focusing on date comparison operations. The tests ensure accurate identification of dates that fall on the previous day using the isYesterday() method with proper date mocking.

Test Coverage Overview

The test coverage focuses on validating the isYesterday plugin’s core functionality for determining if a date represents yesterday.

  • Tests the basic yesterday date check using subtract method
  • Verifies negative case with a specific historical date
  • Implements date mocking for consistent testing

Implementation Analysis

The testing approach utilizes Jest’s testing framework with MockDate for consistent date handling. The implementation follows a clear pattern of setting up mocked dates before tests and cleaning up afterward.

  • Uses beforeEach and afterEach hooks for test isolation
  • Leverages Day.js plugin extension system
  • Implements boolean assertion testing

Technical Details

  • Testing Framework: Jest
  • Mocking Library: MockDate
  • Plugin: isYesterday
  • Setup: Global beforeEach/afterEach hooks
  • Assertion Methods: toBeTruthy(), toBeFalsy()

Best Practices Demonstrated

The test suite demonstrates several testing best practices including proper test isolation and mock management. Each test case is focused and clearly defined, with appropriate setup and teardown procedures.

  • Proper test isolation using mock date reset
  • Clear test case descriptions
  • Efficient plugin integration testing
  • Appropriate use of assertion methods

iamkun/dayjs

test/plugin/isYesterday.test.js

            
import MockDate from 'mockdate'
import dayjs from '../../src'
import isYesterday from '../../src/plugin/isYesterday'

dayjs.extend(isYesterday)

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

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

it('is yesterday', () => {
  expect(dayjs().subtract(1, 'day').isYesterday()).toBeTruthy()
  expect(dayjs('2017-01-01').isYesterday()).toBeFalsy()
})