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

108
Views
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 answers
Answer question

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 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!