As I attempt to create a test suite with date mocking, I encounter an error stating 'Argument of type 'string | Date' is not assignable to parameter of type 'Date''. Below is the function:
const getDay = (inputDate: any, today = new Date()) => {
const reportDate = new Date(inputDate)
if (today >= reportDate) {
const differenceInTime = today.getTime() - reportDate.getTime()
const differenceInDays = differenceInTime / (1000 * 3600 * 24)
const finalDayDiff = Math.floor(differenceInDays)
if (finalDayDiff === 0) {
return translate("common.today")
} else if (finalDayDiff === 1) {
return finalDayDiff + translate("common.dayAgo")
} else {
return finalDayDiff + translate("common.daysAgo")
}
} else {
return ""
}
}
The failing test suite highlights the testName and todayDate in red.
import { getDay } from "../common-functions"
import { translate } from "../../i18n/translate"
describe("checkForDifferenceInDays", () => {
Array.of(
[
"1. difference in days - 1 day ago",
new Date("2021-03-18T00:00:00.000Z"),
1 + translate("common.dayAgo"),
],
[
"2. difference in days - 5 day ago",
new Date("2021-03-22T00:00:00.000Z"),
5 + translate("common.daysAgo"),
],
[
"3. difference in days - today",
new Date("2021-03-17T00:00:00.000Z"),
translate("common.today"),
],
["4. empty string", new Date("2021-03-15T00:00:00.000Z"), ""],
).forEach((testCase) => {
const [testName, todayDate, expectedResult] = testCase
const reportDate = "2021-03-17T00:00:00.000Z"
test(**testName**, () => {
//Act
const actualResult = getDay(reportDate, **todayDate**)
//Assert
expect(actualResult).toEqual(expectedResult)
})
})
})
Highlighted in bold indicates an error. Can someone provide guidance on what I may be missing?