-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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]: Add a way to mock a function only for certain arguments #13811
Comments
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. |
still relevant, I may even have time to make a PR soon |
Agree, this would be super useful. In terms of possible API I suggest looking at jest-mock-extended it implements proper typesafe mocks with support for stubbing matched calls and the API is very straightforward and easy to follow. |
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. |
Still relevant |
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. |
Still active! I have a PR up but need to rethink some details around |
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. |
Still working on this! I haven't had time to get back to the issues above but hope to Soon™. |
I stumbled across this searching for something else, and... wow, I can't believe that jest mocking lacks this basic, essential feature. |
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. |
still haven't had a chance to get back to this, still intend to though! |
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. |
This is a pretty annoying gap in jest. It forces you to lean towards building unit tests based on implementation details rather than behavior. Would be great to get more traction on this |
🚀 Feature Proposal
The idea here is basically equivalent to the
when
helper provided in some third-party packages, but within Jest. It would work similarly tomockImplementation
, but only apply for certain arguments (possibly expect matchers).If there's interest in this,
once my company signs the CLAI may be able to contribute an implementation.Motivation
This seems to be a quite common need, given the several third-party packages and in my own experience. (See also the pitch below.)
Example
There are a few possible APIs, but the most likely one is similar to jest-mock-extended and jest-when.
The method
mockWhen
would return a jest mock which is used only when the arguments match, but can be customized in all the usual ways.Pitch
This is a feature that many test frameworks and tools across many languages seem to grow, which points to its utility!
There are a few alternatives to doing this in Jest, which all seem inferior:
when
implementations is that they don't use the normal Jest API; they instead provide their own which is a bit inconsistent, and there are several different packages people use which leads to fragmentation. Plus, implementing this within Jest could use the existing machinery and thus actually be a lot simpler, and could have the API be a method alongsidemockImplementation
.mockImplementation
or via.mock.calls
). This is a bit more work, and often results in more fragile tests, since if other code executed in the test makes an unrelated call to the function, it will fail the test. (And if we already have several such calls, this method no longer works.)mockReturnValue
), and not bother asserting anything further; this leads to weaker tests as well as not allowing different results for different arguments.if (arg === ...) return someValue; return otherValue
); this is a lot more work in most simple cases -- arguably it's so much work no one will bother.The text was updated successfully, but these errors were encountered: