【发布时间】:2020-07-21 07:32:38
【问题描述】:
我正在开玩笑地覆盖这样的jsdom localstorage API,以便测试我的try catch代码。
Storage.prototype.setItem = jest.fn(() => {
throw new Error("error");
});
如何重置其实现?
我目前正在这样做。有没有更清洁的方法来做到这一点?
const setImplementation = Storage.prototype.setItem;
Storage.prototype.setItem = jest.fn(() => {
throw new Error("error");
});
expect(() => {
localStorageHelper.setData(key, value);
}).not.toThrow();
Storage.prototype.setItem = setImplementation;
done();
【问题讨论】: