Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it still valid to mock read only properties from the window object using cy.stub? #765

Open
Leonelmarianog opened this issue Feb 24, 2022 · 0 comments

Comments

@Leonelmarianog
Copy link

Leonelmarianog commented Feb 24, 2022

The following example is copied from this issue, it shows how to stub navigator.cookieEnabled.

cy.visit("http://localhost:3000", {
  onBeforeLoad(win) {
    cy.stub(win.navigator, "cookieEnabled", false); // works fine
  },
});

This example still works today with Cypress 9.5.0 but my code editor warns me it is deprecated (and since I'm using TypeScript, it expects a function as a third parameter instead of a single value). I tried using the syntax show on the documentation but I can't make it work.

cy.visit("http://localhost:3000", {
  onBeforeLoad(win) {
    cy.stub(win.navigator, "cookieEnabled").returns(false); // Doesn't do anything
  },
});

I guess this is because cy.stub(...).returns(value) is supposed to be used with functions/methods instead of simple properties?. Is there any other way to do this today?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant