How to mock/replace getter function of object with Jest?

前端 未结 3 1283
小鲜肉
小鲜肉 2021-02-03 17:44

In Sinon I can do the following:

var myObj = {
    prop: \'foo\'
};

sinon.stub(myObj, \'prop\').get(function getterFn() {
    return \'bar\';
});

myObj.prop; /         


        
3条回答
  •  深忆病人
    2021-02-03 18:12

    You could use Object.defineProperty

    Object.defineProperty(myObj, 'prop', {
      get: jest.fn(() => 'bar'),
      set: jest.fn()
    });
    

提交回复
热议问题