jest-mock-extended icon indicating copy to clipboard operation
jest-mock-extended copied to clipboard

No simple getter mocking planned yet?

Open Sebastriani opened this issue 3 years ago • 6 comments
trafficstars

It would be a nice feature to have since the solution proposed here #29 is kind of cumbersome...

Sebastriani avatar Mar 14 '22 06:03 Sebastriani

I would like to second on that.

The library is very neat to use, however mocking getters using Object.defineProperty() is simply yuk :( It looks more like ugly hack rather than good default way of dealing with things.

krzychuz avatar Jun 24 '22 10:06 krzychuz

the built-in spyOn() method is now able to mock getters / setters ie. jest.spyOn(video, 'play', 'get')

https://jestjs.io/docs/jest-object#jestspyonobject-methodname-accesstype

knicola avatar Jul 03 '22 06:07 knicola

the built-in spyOn() method is now able to mock getters / setters ie. jest.spyOn(video, 'play', 'get')

Could you expand on that a little? I thought you meant I could do something like this:

const req = mock<Request>();
const params = jest.spyOn(req, "params", "get");

But that gives me the error "params property does not exist" (which is definitely not true - it's there, and it's a getter).

roblframpton avatar Aug 05 '22 16:08 roblframpton

@roblframpton , I stumbled upon exactly the same issue. As mentioned above - the only workaround is to use Object.defineProperty()

krzychuz avatar Jan 27 '23 10:01 krzychuz

I also get a runtime error :

Error: Property foo does not exist in the provided object

Would be really good for Jest mock extended to allow you to mock properties. Its a big missing feature atm

The suggested work around to use Object.defineProperty is really ugly and cumbersome

gusgorman2 avatar Apr 22 '24 15:04 gusgorman2