cy.window().then(win => {
cy.spy(win.location, 'replace').as('locationReplace');
});
Or:
cy.window().then(win => {
cy.stub(win.location, 'replace').as('locationReplace');
});
Now throws an error TypeError: Cannot redefine property: replace
location methods can be replaced with stubs or be spyed.
Just try this at any website.
OS: osx 10.14.3
Cypress: 3.1.5
Browser: Chrome 72.0.3626.119
window.location is read-only, so that's why you're getting this error. The browser won't allow you to modify this object.
Are you trying to assert on a location change? Have you tried using the cy.url() or cy.location() APIs for your needs?
Example of using cy.url() to assert on location change (from the docs):
// clicking the anchor causes the browser to follow the link
cy.get('#user-edit a').click()
cy.url().should('include', '/users/1/edit') // => true
cy.url().should('eq', 'http://localhost:8000/users/1/edit') // => true
Closing as resolved. Please comment if you are still having this issue and we will consider reopening.
How can we do this with locations on a different host?
Most helpful comment
How can we do this with locations on a different host?