Back to Repositories

Validating Plugin Extension System in Day.js

This test suite validates the plugin extension functionality in Day.js, focusing on how plugins can extend both instance and static methods. It verifies the core plugin architecture and utility access mechanisms.

Test Coverage Overview

The test suite provides comprehensive coverage of Day.js plugin system functionality.

Key areas tested include:
  • Plugin extension mechanism for instance methods
  • Static method extension capabilities
  • Plugin configuration options handling
  • Access to core utility functions
  • Mock date handling for consistent testing

Implementation Analysis

The testing approach utilizes Jest’s modular testing patterns with MockDate for consistent datetime handling. The implementation demonstrates both basic and configured plugin extensions, verifying the plugin architecture’s flexibility.

Technical patterns include:
  • Before/after hooks for date mocking
  • Prototype-based method extension testing
  • Static function addition verification
  • Configuration parameter validation

Technical Details

Testing infrastructure includes:
  • Jest as the primary testing framework
  • MockDate for date manipulation
  • Custom plugin test helpers
  • ES6 module import system
  • Prototype-based extension patterns

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through modular test organization and comprehensive coverage.

Notable practices include:
  • Isolated test cases for each plugin feature
  • Consistent test environment setup/teardown
  • Clear test case descriptions
  • Verification of both success paths and utility access
  • Proper mock handling and cleanup

iamkun/dayjs

test/plugin.test.js

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

const testPlugin = (o, c, d) => {
  c.prototype.newApi = () => ('hello world')
  d.newFunc = () => ('hi world')
}
const testPluginWithConfig = (o, c) => {
  c.prototype.newApiWithConfig = () => (`hello world ${o || ''}`)
}

dayjs.extend(testPlugin)
dayjs.extend(testPluginWithConfig, 'good')

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

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

it('Plugin extend method and option', () => {
  expect(dayjs().newApi()).toBe('hello world')
  expect(dayjs().newApiWithConfig()).toBe('hello world good')
})

it('Plugin extend dayjs', () => {
  expect(dayjs.newFunc()).toBe('hi world')
})

it('Plugin use core utils', () => {
  // u => isUndefined
  expect(dayjs().$utils().u).toBeInstanceOf(Function)
})