Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No simple getter mocking planned yet? #84

Open
Sebastriani opened this issue Mar 14, 2022 · 5 comments
Open

No simple getter mocking planned yet? #84

Sebastriani opened this issue Mar 14, 2022 · 5 comments

Comments

@Sebastriani
Copy link

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

@krzychuz
Copy link

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.

@knicola
Copy link

knicola commented Jul 3, 2022

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

@roblframpton
Copy link

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).

@krzychuz
Copy link

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

@gusgorman2
Copy link

gusgorman2 commented Apr 22, 2024

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

Beraliv pushed a commit to Beraliv/jest-mock-extended that referenced this issue May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants