[英]Jest unit test fails but it shouldnt
I have this method that returns Boolean.我有这个方法返回 Boolean。 It works as it should but in unit tests it fails.
它可以正常工作,但在单元测试中它失败了。 The method looks like this:
该方法如下所示:
const isRushHour = (date: string): boolean => {
let isRushHour: boolean = false;
let isFriday: boolean = new Date(date).getDay() === 5;
let selectedTime: string = new Date(date).toLocaleTimeString(
navigator.language,
{
hour: "2-digit",
minute: "2-digit",
}
);
let isRushHourRange: boolean =
selectedTime >= "15:00" &&
selectedTime <= "19:00" ;
if (isFriday && isRushHourRange) {
isRushHour = true;
}
return isRushHour;
};
it Basically checks if date is friday and if it its between the time range.它基本上检查日期是否是星期五以及它是否在时间范围之间。 However everything is fine when checking with console f.ex;
但是,使用控制台 f.ex 进行检查时一切正常;
console.log(isRushHour("2022-01-07T19:00"))
This will return true and it should be like this !这将返回 true 并且应该是这样的!
But in jest i tried with .toBe(true), .toBeTruthy(), and to toEqual(true)
但开玩笑的是,我尝试使用
.toBe(true), .toBeTruthy(), and to toEqual(true)
expect(calculator.isRushHour("2022-01-07T19:00")).toBe(true);
Exactly the same string.完全相同的字符串。
But the test fails with message:但是测试失败并显示消息:
expect(received).toBe(expected) // Object.is equality
Expected: true
Received: false
This fixed the issue:这解决了这个问题:
beforeEach(() => {
jest.spyOn(window.navigator, "language", "get");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.