我有这个单元测试:
import { describe, expect, it } from "@jest/globals"
import { format2APIDate } from "../helpers"
describe("format2APIDate()", () => {
it("should return a date with the format: YYYY/MM/DD for date with this format YYYY-MM-DD", () => {
const date = new Date("2024-02-12")
const formattedDate = format2APIDate(date)
expect(formattedDate).toBe("2024/02/12")
})
it("should return a date with the format: YYYY/MM/DD when a date has this format", () => {
const date = new Date("Mon Feb 12 2024 00:00:00 GMT+0100 (Central European Standard Time)")
const formattedDate = format2APIDate(date)
expect(formattedDate).toBe("2024/02/12")
})
})
当我在本地运行测试时:
npm run test
。
他们通过了。
但是,当我合并更改时,最后一个测试失败:
这是 format2APIDate:
export const format2APIDate = (date: Date) => {
// Format the date as "YYYY/MM/DD"
const formattedDate = new Date(date)
return `${formattedDate.getFullYear()}/${String(formattedDate.getMonth() + 1).padStart(2, "0")}/${String(formattedDate.getDate()).padStart(2, "0")}`
}