Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Jest unit test fails but it shouldnt

I have this method that returns 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;

console.log(isRushHour("2022-01-07T19:00"))

This will return true and it should be like this !

But in jest i tried with .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
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This fixed the issue:

   beforeEach(() => {
  jest.spyOn(window.navigator, "language", "get");
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!