Skip to content

Commit 269a7a9

Browse files
authored
fix: added usage warnings for diff + updated unit tests (#2948)
1 parent 9e3132e commit 269a7a9

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/plugin/devHelper/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ export default (o, c, d) => {
2626
}
2727
return oldLocale(preset, object, isLocal)
2828
}
29+
30+
const oldDiff = proto.diff
31+
proto.diff = function (date, unit, float) {
32+
const isInvalidDate = !date || !d(date).isValid()
33+
if (isInvalidDate) {
34+
console.warn('Invalid usage: diff() requires a valid comparison date as the first argument. https://day.js.org/docs/en/display/difference')
35+
}
36+
37+
return oldDiff.call(this, date, unit, float)
38+
}
2939
}
3040
}
31-

test/plugin/devHelper.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import MockDate from 'mockdate'
22
import dayjs from '../../src'
3+
import customParseFormat from '../../src/plugin/customParseFormat'
34
import devHelper from '../../src/plugin/devHelper'
5+
import localeData from '../../src/plugin/localeData'
46

57
dayjs.extend(devHelper)
68

@@ -38,3 +40,46 @@ it('Warning: Setting locale before loading locale', () => {
3840
dayjs.locale('zh-cn')
3941
expect(consoleSpy).toHaveBeenCalledWith('Guessing you may want to use locale zh-cn, you have to load it before using it. https://day.js.org/docs/en/i18n/loading-into-nodejs')
4042
})
43+
44+
describe('dev-helper: diff() usage warnings', () => {
45+
const diffWarningMsg = 'Invalid usage: diff() requires a valid comparison date as the first argument. https://day.js.org/docs/en/display/difference'
46+
47+
beforeAll(() => {
48+
dayjs.extend(customParseFormat)
49+
dayjs.extend(localeData)
50+
})
51+
52+
beforeEach(() => {
53+
jest.clearAllMocks()
54+
})
55+
56+
it('warns when diff() is called with no comparison date', () => {
57+
const consoleSpy = jest.spyOn(console, 'warn')
58+
dayjs('2025-01-10').diff()
59+
expect(consoleSpy).toHaveBeenCalledWith(diffWarningMsg)
60+
})
61+
62+
it('warns when diff() is called with just the unit', () => {
63+
const consoleSpy = jest.spyOn(console, 'warn')
64+
dayjs('2025-01-10').diff('days')
65+
expect(consoleSpy).toHaveBeenCalledWith(diffWarningMsg)
66+
})
67+
68+
it('warns when diff() is called with an invalid comparison date (unparsable string)', () => {
69+
const consoleSpy = jest.spyOn(console, 'warn')
70+
dayjs('2025-01-10').diff('invalid-date', 'days')
71+
expect(consoleSpy).toHaveBeenCalledWith(diffWarningMsg)
72+
})
73+
74+
it('does NOT warn when diff() is called with a valid string date', () => {
75+
const consoleSpy = jest.spyOn(console, 'warn')
76+
dayjs('2025-01-10').diff('2025-01-09', 'days')
77+
expect(consoleSpy).not.toHaveBeenCalledWith(diffWarningMsg)
78+
})
79+
80+
it('does NOT warn when diff() is called with a valid Day.js instance', () => {
81+
const consoleSpy = jest.spyOn(console, 'warn')
82+
dayjs('2025-01-10').diff(dayjs(), 'days')
83+
expect(consoleSpy).not.toHaveBeenCalledWith(diffWarningMsg)
84+
})
85+
})

0 commit comments

Comments
 (0)