【发布时间】:2021-06-15 14:43:40
【问题描述】:
我在测试一天的间隔时遇到了困难。 也许有人可以解释我做错了什么。
这就是我要测试的内容
export const getMondayFromDay = (date) => {
const monday = new Date(date);
const day = monday.getDay();
monday.setDate(monday.getDate() - day + (day === 0 ? -6 : 1));
monday.setHours(0, 0, 0, 0);
return monday;
}
我的失败测试
it("returns day interval from monday", () => {
const mockMondayFromDay = new Date('2021-01-04');
const expectedFrom = new Date('2021-01-04');
expectedFrom.setHours(0, 0, 0, 0);
const expectedTo = new Date('2021-01-04');
expectedTo.setHours(23, 59, 59, 999);
jest.fn(() => mockMondayFromDay);
const { from , to } = getMondayFromDay(new Date('2021-01-04'));
expect(from).toEqual(expectedFrom);
expect(to).toEqual(expectedTo);
});
【问题讨论】:
标签: javascript unit-testing jestjs