Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  115] [ 1]  / answers: 1 / hits: 88460  / 7 Years ago, sat, april 29, 2017, 12:00:00

In Sinon I can do the following:



var myObj = {
prop: 'foo'
};

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

myObj.prop; // 'bar'


But how can I do the same with Jest?
I can't just overwrite the function with something like jest.fn(), because it won't replace the getter




can't set the value of get



More From » reactjs

 Answers
10

You could use Object.defineProperty



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

[#57965] Wednesday, April 26, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
robinh

Total Points: 371
Total Questions: 105
Total Answers: 89

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
robinh questions
Thu, May 6, 21, 00:00, 3 Years ago
Thu, Jan 7, 21, 00:00, 3 Years ago
Fri, Nov 6, 20, 00:00, 4 Years ago
Sun, Aug 9, 20, 00:00, 4 Years ago
;