在 Sinon 我可以做以下事情:
var myObj = {
prop: 'foo'
};
sinon.stub(myObj, 'prop').get(function getterFn() {
return 'bar';
});
myObj.prop; // 'bar'
但是我怎样才能对 Jest 做同样的事情呢?我不能只用类似 jest.fn()
的东西覆盖函数,因为它不会取代 getter
“无法设置 get 的值”
原文由 I_like_foxes 发布,翻译遵循 CC BY-SA 4.0 许可协议
你可以使用
Object.defineProperty