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
[Feature Request] Extend vi.fn
#756
Comments
I like the idea.
I really like Moq API https://github.com/Moq/moq4/wiki/Quickstart but I'm not sure how much can be ported to JS land. |
I like the idea of About an API to define the mapping, maybe we should leave that for user-land? Because your example could be a stand alone utility that generates the implementation: vi.fn(
mon().mockReturn(null)
.on('3').mockReturn(true)
.on('4').mockReturn(false)
) Or it could be based on an array vi.fn(mockFrom([
{ return: null },
{ when: '3', return: true },
{ when: '4', return: false }
]) Maybe when we get pattern matching in JS users could reach for it https://github.com/tc39/proposal-pattern-matching? I kind of like the simplicity of a function and letting the user do whatever they want inside here. |
+1 for making it a separate library. It would be useful with other test runners too. By the way, there it a |
-1 for a separate package (or we should at least use it by default) |
Co-authored-by: eliarege <ege.iliklier@eliarge.com>
I found this issue when looking for a Vitest based alternative for https://github.com/timkindberg/jest-when. I guess I have to migrate to a list of ifs, but hopefully this library can serve as an inspiration. |
i'm also looking for a vitest alternative for an api like |
I'm pretty sure you can use |
i hoped for the same the last time i tried, but ran into errors when attempting to incorporate it. it has been a bit since i've attempted, so i'll try it again and bring back some detail of the error specifics. |
Yeah, looking at the source code you might have errors. #1956 is meant to fix this, but we haven't released new version yet |
great to hear! i'll watch for the next release and give it a shot. thanks! |
with v0.23.0 available, i was able to use i did have to enable globals for it to work, but seems to work as expected after that. thanks! |
Replying to this issue as we are in the process of refactoring a jest codebase into vitest. One of the issues we found is jest-when is used everywhere throughout our test files, which we don't want to refactor out yet, but Temporary fix is patching the |
Clear and concise description of the problem
We previously had a disscussion that it would be usefull to have a method on
vi.fn
that will defined behaviour when the function is called with this arguments.Suggested solution
For example:
Instead of this
We can wright something like this (API is not finalized):
Also does anyone else think that
vi.fn
API is too verbose? Maybe we can tighten it a bit. Would love to see suggestions and thoughts. For example:vi.fn().returns(true)
instead ofvi.fn().mockReturnValue(true)
vi.fn().rejects(new Error())
instead ofvi.fn().mockRejectValue(new Error())
vi.fn().resolves(true)
instead ofvi.fn().mockResolveValue(true)
This issue is open for other extensions. I would like to see all of the problems people face and create issues accordingly.
The text was updated successfully, but these errors were encountered: