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

149
Views
window.location.href updates only after the test is done in react

I use useNavigate from react-router-dom to handle navigation. I called a useNavigate hook at the top level of the functional component like that: const navigate = useNavigate(). Inside a handleClick() function I use it to navigate to an another page of my website like that: navigate("/home"). I tried it and it is working fine, if I click on the button it takes me to homepage (http://localhost/home). However I have problems unit testing this functionality. After simulating a click on that button, the url should change so I wrote the following expectation: expect(global.window.location.href).toContain('/home');

The test fails, the window location did not change. However when I tried to console log the window location in the afterEach() method, it shows that after the test the global.window.location.href equals to http://localhost/home. So it is working just can`t verify that in my test....

Why it is only change after the test? What can I do to verify that url inside the test?

about 4 years ago · Juan Pablo Isaza
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!