-
Notifications
You must be signed in to change notification settings - Fork 11
relativeDateToDateRange test fails for 'month' with days < current month days #761
Description
For utils.test.ts, expect(m).toBeLessThan(curMonth); test fails on occasion:
hub.js/packages/common/test/search/utils.test.ts
Lines 200 to 216 in 43c9997
| it("converts months", () => { | |
| const rd: IRelativeDate = { | |
| type: "relative-date", | |
| num: 1, | |
| unit: "months", | |
| }; | |
| const chk = relativeDateToDateRange(rd); | |
| expect(chk.from).toBeLessThan(chk.to); | |
| const m = new Date(chk.from).getMonth(); | |
| const curMonth = new Date().getMonth(); | |
| // Account for January/December | |
| if (curMonth > 0) { | |
| expect(m).toBeLessThan(curMonth); | |
| } else { | |
| expect(m).toBe(11); | |
| } | |
| }); |
I believe the reason is because it is attempting to get the number of days in the current month and apply that to the from month. I tried to run this test today March 30 and attempting to get a 1 month range returns March 02 (since there are 28 days in February in 2022).
Here is the where we are subtracting relative.num the current month:
hub.js/packages/common/src/search/utils.ts
Lines 219 to 223 in 43c9997
| case "months": | |
| // get the current month and subtract num | |
| now.setMonth(now.getMonth() - relative.num); | |
| result.from = now.getTime(); | |
| break; |
I think we can either update the test to also check for equality expect(m).toBeLessThanOrEqual(curMonth), or adjust relativeDateToRange to roll to previous month in these edge cases.