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

112
Vistas
Cypress - mocking window property

I have a code that uses window.foo.abc as a condition to display something.

I want to test this functionality with cypress and I want to mock this value to be false and true.

How can I do that?

I've tried

 before(function() {
      Cypress.on('window:before:load', win => {
        win.foo.abc = true;
      });

and

  cy.window().then(win => {
    window.foo.abc = true;
  });

with no success.

How can I mock this value?

thanks 🙏

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This code is incorrect,

Cypress.on('window:before:load', win => {
  window.foo.abc = true;
});

It should be

Cypress.on('window:before:load', win => {
  win.foo.abc = true;
});

You don't have to use it in before(), but it should be at the top of the spec.

But I suspect it still won't work after correcting, most likely the app resets foo to a new object during loading, i.e during cy.visit()

You can use the 2nd block

cy.visit('...')  // visit before changing

cy.window().then(win => {
  win.foo.abc = true;            // correct the syntax here as well
})
about 4 years ago · Juan Pablo Isaza Denunciar
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