Skip to content
This repository was archived by the owner on Nov 19, 2025. It is now read-only.
This repository was archived by the owner on Nov 19, 2025. It is now read-only.

relativeDateToDateRange test fails for 'month' with days < current month days #761

@tannerjt

Description

@tannerjt

For utils.test.ts, expect(m).toBeLessThan(curMonth); test fails on occasion:

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions