Ky Lane
Ky Lane

Reputation: 338

Cypress: Can I stub/mock the window.location.hostname in a test?

I have a component that should not appear on a list of hostnames, and want a cypress test to test the logic around that.

I know with my Jest unit tests, I can use delete global.window.location.hostname then global.window.location.hostname = "whatever" but can't seem to find a Cypress equivalent.
(Ref How to mock window.location.href with Jest + Vuejs)

Any help appreciated.

Upvotes: 4

Views: 3881

Answers (1)

user14783414
user14783414

Reputation:

Cypress has the cy.location() command, but there appears to be a getter but no setter.

You could try

cy.window().then(win => win.location.hostname = 'newvalue');

or

cy.state('window').location.hostname = 'newvalue';

No need to delete it first.

Upvotes: 0

Related Questions