Skip to content

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

@Leonelmarianog

Description

@Leonelmarianog

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?.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions