-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 QA: Add test for getStartOf edge case #338
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
base: staging
Are you sure you want to change the base?
Changes from all commits
b23cba4
0662652
235b001
f33c6b8
6d7b726
b33c68e
fabe6db
129f1d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||||||
| import { describe, it, expect } from 'vitest' | ||||||||||||||||||||||||||||||||||||||||||
| import { formatDuration, isValidDate } from './formatDate' | ||||||||||||||||||||||||||||||||||||||||||
| import { formatDuration, isValidDate, getStartOf } from './formatDate' | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| describe('formatDuration', () => { | ||||||||||||||||||||||||||||||||||||||||||
| it('formats seconds correctly', () => { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -60,4 +60,14 @@ describe('isValidDate', () => { | |||||||||||||||||||||||||||||||||||||||||
| expect(isValidDate('2023-01-01T00:00:00+05:00')).toBe(true) | ||||||||||||||||||||||||||||||||||||||||||
| expect(isValidDate('2023-01-01T00:00:00-08:00')).toBe(true) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| describe('getStartOf', () => { | ||||||||||||||||||||||||||||||||||||||||||
| it('returns the start of the day', () => { | ||||||||||||||||||||||||||||||||||||||||||
| const date = new Date('2023-05-15T12:30:45Z'); | ||||||||||||||||||||||||||||||||||||||||||
| const start = getStartOf(date, 'day'); | ||||||||||||||||||||||||||||||||||||||||||
| expect(start.getHours()).toBe(0); | ||||||||||||||||||||||||||||||||||||||||||
| expect(start.getMinutes()).toBe(0); | ||||||||||||||||||||||||||||||||||||||||||
| expect(start.getSeconds()).toBe(0); | ||||||||||||||||||||||||||||||||||||||||||
| expect(start.getMilliseconds()).toBe(0); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+68
to
+71
|
||||||||||||||||||||||||||||||||||||||||||
| expect(start.getHours()).toBe(0); | |
| expect(start.getMinutes()).toBe(0); | |
| expect(start.getSeconds()).toBe(0); | |
| expect(start.getMilliseconds()).toBe(0); | |
| expect(start.getUTCHours()).toBe(0); | |
| expect(start.getUTCMinutes()).toBe(0); | |
| expect(start.getUTCSeconds()).toBe(0); | |
| expect(start.getUTCMilliseconds()).toBe(0); |
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semicolons were introduced in the new test, but the surrounding file uses a no-semicolons style. For consistency and easier maintenance, align the new lines with the existing formatting conventions in this test file.
| const date = new Date('2023-05-15T12:30:45Z'); | |
| const start = getStartOf(date, 'day'); | |
| expect(start.getHours()).toBe(0); | |
| expect(start.getMinutes()).toBe(0); | |
| expect(start.getSeconds()).toBe(0); | |
| expect(start.getMilliseconds()).toBe(0); | |
| const date = new Date('2023-05-15T12:30:45Z') | |
| const start = getStartOf(date, 'day') | |
| expect(start.getHours()).toBe(0) | |
| expect(start.getMinutes()).toBe(0) | |
| expect(start.getSeconds()).toBe(0) | |
| expect(start.getMilliseconds()).toBe(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test mixes a UTC input ('...Z') with local-time assertions (getHours()/getMinutes()/etc). If getStartOf is implemented using UTC setters/getters (setUTCHours/getUTCHours), this becomes environment-dependent and can be flaky across time zones/CI settings. Also, asserting only the time fields can miss a bug where the day shifts unexpectedly (still 00:00:00.000, but on the wrong date).
Suggestion
Make the expectation deterministic and explicit about the intended semantics (local vs UTC). A robust pattern is to compute the expected value using the same JS semantics and compare timestamps:
it('returns the start of the day', () => {
const date = new Date('2023-05-15T12:30:45Z')
const expected = new Date(date)
expected.setHours(0, 0, 0, 0) // local start-of-day
const start = getStartOf(date, 'day')
expect(start.getTime()).toBe(expected.getTime())
})If getStartOf is intended to be UTC-based, use setUTCHours(0,0,0,0) and assert with getTime() similarly.
Reply with "@CharlieHelps yes please" if you’d like me to add a commit with this change.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: New
getStartOftest is incomplete: it only checks time fields and does not verify the returned date/day, allowing wrong-day midnight results to pass.Prompt for AI agents