【发布时间】:2019-12-04 03:23:13
【问题描述】:
刚刚在我的 jest 配置中升级到 jsdom-14。它工作得很好,但是一个测试失败了。
test('Do the thing', () => {
window.location.assign = jest.fn();
});
我继承了这个代码。它看起来像一个足够简单的笑话模拟。它抱怨它无法分配只读属性assign,这是有道理的,我认为这是添加的 jsdom 功能。
但是...我也不能做jest.spyOn,这似乎是建议的。我以前没用过 spyOn。
jest.spyOn(window.location.assign);
但这给了我一个未定义的属性错误:
Cannot spy the undefined property because it is not a function; undefined given instead
在此之前的行,我添加了一个日志只是为了检查。绝对是一个函数:
console.log(window.location.assign);
=> [Function: assign]
我不确定这两个错误如何共存 - 已定义和未定义?
【问题讨论】:
-
尝试
delete这个属性(delete window.location.assign)然后重新添加。