From 3af36ca02adecd4d1215fb814c7b1b34bef7b5b4 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 6 Nov 2020 10:47:37 +0800 Subject: [PATCH] fix: fix utc plugin diff edge case --- src/plugin/utc/index.js | 2 +- test/display.test.js | 4 ++++ test/plugin/utc.test.js | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 5fb049793..b5b5f4e64 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -112,7 +112,7 @@ export default (option, Dayjs, dayjs) => { } const oldDiff = proto.diff proto.diff = function (input, units, float) { - if (this.$u === input.$u) { + if (input && this.$u === input.$u) { return oldDiff.call(this, input, units, float) } const localThis = this.local() diff --git a/test/display.test.js b/test/display.test.js index 42975c3cb..7eb48fe82 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -215,6 +215,10 @@ describe('Difference', () => { expect(dayjs('2018-08-08').diff(dayjs('2018-09-08'), 'month')).toEqual(-1) expect(dayjs('2018-01-01').diff(dayjs('2018-01-01'), 'month')).toEqual(0) }) + + it('undefined edge case', () => { + expect(dayjs().diff(undefined, 'seconds')).toBeDefined() + }) }) it('Unix Timestamp (milliseconds)', () => { diff --git a/test/plugin/utc.test.js b/test/plugin/utc.test.js index 7ef563cd9..af046cfea 100644 --- a/test/plugin/utc.test.js +++ b/test/plugin/utc.test.js @@ -266,3 +266,7 @@ it('utc keepLocalTime', () => { expect(dd).toEqual(dm) expect(vd).toEqual(vm) }) + +it('utc diff undefined edge case', () => { + expect(dayjs().diff(undefined, 'seconds')).toBeDefined() +})