Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda