Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update devHelper add warning "passing Year as a Number will be parsed as a Unix timestamp" #1315

Merged
merged 1 commit into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/plugin/devHelper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export default (o, c, d) => {
if (typeof date === 'string' && date.length === 13) {
console.warn(`To parse a Unix timestamp like ${date}, you should pass it as a Number. https://day.js.org/docs/en/parse/unix-timestamp-milliseconds`)
}
if (typeof date === 'number' && String(date).length === 4) {
console.warn(`Guessing you may want to parse the Year ${date}, you should pass it as a String ${date}, not a Number. Otherwise, ${date} will be treated as a Unix timestamp`)
}
if (cfg.args.length >= 2 && !d.p.customParseFormat) {
console.warn(`To parse a date-time string like ${date} using the given format, you should enable customParseFormat plugin first. https://day.js.org/docs/en/parse/string-format`)
}
Expand Down
34 changes: 34 additions & 0 deletions test/plugin/devHelper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import MockDate from 'mockdate'
import dayjs from '../../src'
import devHelper from '../../src/plugin/devHelper'

dayjs.extend(devHelper)

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

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

global.console.warn = jest.genMockFunction()


it('Warning: passing Year as a Number will be parsed as a Unix timestamp', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs(2020)
expect(consoleSpy).toHaveBeenCalledWith('Guessing you may want to parse the Year 2020, you should pass it as a String 2020, not a Number. Otherwise, 2020 will be treated as a Unix timestamp')
})

it('Warning Passing Unix timestamp as a String not Number', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('1231231231231')
expect(consoleSpy).toHaveBeenCalledWith('To parse a Unix timestamp like 1231231231231, you should pass it as a Number. https://day.js.org/docs/en/parse/unix-timestamp-milliseconds')
})

it('Warning Enable customParseFormat plugin while passing the second format parameter', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('2020', 'YYYY')
expect(consoleSpy).toHaveBeenCalledWith('To parse a date-time string like 2020 using the given format, you should enable customParseFormat plugin first. https://day.js.org/docs/en/parse/string-format')
})