Cypress: TypeError: Cannot redefine property: replace

Created on 5 Mar 2019  路  3Comments  路  Source: cypress-io/cypress

Current behavior:

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

Desired behavior:

location methods can be replaced with stubs or be spyed.

Steps to reproduce: (app code and test code)

Just try this at any website.

Versions

OS: osx 10.14.3
Cypress: 3.1.5
Browser: Chrome 72.0.3626.119

Most helpful comment

How can we do this with locations on a different host?

All 3 comments

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?

Was this page helpful?
0 / 5 - 0 ratings