目前,我正在为我的项目实施单元测试,并且有一个文件包含 window.location.href
。
我想模拟这个来测试,这是我的示例代码:
it("method A should work correctly", () => {
const url = "http://dummy.com";
Object.defineProperty(window.location, "href", {
value: url,
writable: true
});
const data = {
id: "123",
name: null
};
window.location.href = url;
wrapper.vm.methodA(data);
expect(window.location.href).toEqual(url);
});
但我得到这个错误:
TypeError: Cannot redefine property: href
at Function.defineProperty (<anonymous>)
我尝试了一些解决方案,但没有解决。我需要一些提示来帮助我摆脱这个麻烦。请帮忙。
原文由 Tran Son Hoang 发布,翻译遵循 CC BY-SA 4.0 许可协议
你可以试试:
查看该问题的 Jest Issue:
开玩笑的问题