【发布时间】:2021-04-02 18:25:05
【问题描述】:
我试图通过模拟日期来编写测试套件。我得到这个错误'Argument of type 'string |日期'不可分配给'日期'类型的参数。'。这是函数
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 ""
}
}
失败的测试套件 testName 和 todayDate 以红色突出显示
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 shows an error. can someone help what i am missing
【问题讨论】:
标签: javascript typescript jestjs