简体   繁体   English

赛普拉斯:我可以在测试中存根/模拟 window.location.hostname 吗?

[英]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.我有一个不应该出现在主机名列表中的组件,并且想要一个 cypress 测试来测试围绕它的逻辑。

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.我知道我的 Jest 单元测试,我可以使用delete global.window.location.hostname然后global.window.location.hostname = "whatever"但似乎找不到赛普拉斯等价物。
(Ref How to mock window.location.href with Jest + Vuejs ) (参考如何使用 Jest + Vuejs 模拟 window.location.href

Any help appreciated.任何帮助表示赞赏。

Cypress has the cy.location() command, but there appears to be a getter but no setter.赛普拉斯有cy.location()命令,但似乎有一个 getter 但没有 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.无需先删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM