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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]: Replace property value in tests #13465
Comments
This seems reasonable to me.
Agreed. We currently return mock functions. I guess we could add a proxy and count accesses or something? that way it'd still be a "mock object" of sorts? Would you be up for sending a PR? I cannot make any promises it'll land, but looking at code might make it easier to decide if this feature fits into Jest? |
I can try something. If you have any thoughts upfront, I'd appreciate them. I have been looking at the existing implementation of |
You can start with playing around in |
It's better to name it with 'spyOn', because it's a bit different variation of spy, IMHO. |
When you'll have time, you might take a look at draft PR #13496, which contains complete implementation of I have been thinking about reusing same interface and allow spying property access. Then I discarded the idea (reasons below) and decided just to provide simple property value replacement. Why I think, that spying on property access is not ideal idea:
I will be really interested in your thoughts if this is Thank you a lot! |
I've opted for |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Struggled with this scenario a lot and Would be great if we can move this forward. #13496 is still in draft though it looks ready for review to me? |
@eps1lon I've improved the PR, it's not draft anymore. I will be looking forward for your feedback. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
馃殌 Feature Proposal
It would be great to have interface to replace object's property value as part of Jest API.
Similar to Sinon's
sandbox.replace(object, property, replacement);
Ref: https://sinonjs.org/releases/v14/sandbox/#sandboxreplaceobject-property-replacement
Motivation
Quite often there are parts of an object, we have to mock in tests. For example
process.argv
,process.env
or some configuration settings coming out of whichever piece in codebase.Usually we end up writing code like:
Problem is, that for trivial property replace, one needs to split it across multiple pieces, store old value and then properly return it. This might be cause for errors.
Example
Property would be replaced back by calling
jest.restoreAllMocks();
It is questionable, what should the mock return. If it should return something to intercept the calls and other stuff (probably yes, but I do not have opinion on that).
Pitch
Jest provides great and extensive way for mocking modules and methods. One piece, that is missing, is mocking individual properties for arbitrary object.
Of course, this is doable manually. However the biggest benefit of integrating this functionality to Jest API would be the integration into 'autoclean' feature that will simplify the boilerplate for individual property mocking, and thus making the code more error prone.
The text was updated successfully, but these errors were encountered: