-
-
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
Testing function context ("this") in Mock functions #11235
Comments
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
I don't think we should add matchers for this. but adding it as a |
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
Currently Jest doesn't have a strong story for how to test the context of mock function calls.
Motivation
It's well-documented how to test other aspects of calls (arguments and return values), but not the context.
Note: the "context" is the "this" binding which can be set with
Function.prototype.call
orbind
:It is currently possible to test the context with
mock.instances
, but this doesn't seem to be the intended purpose. According to the docsmock.instances
seems class-related:In fact, there is an open issue (#7070) discussing how to "fix"
instances
to only be the return value ofnew
calls. If this happens, there will be no way to test the context of regular calls.In any case, it would be nice to see context get official support and documentation. Even better if it could get a dedicated expectation clause like
toHaveBeenCalledWithContext
.Example
Ideas:
Pitch
Context is core to functions. While it may be a lesser-used pattern, it has been around for a long time, in libraries like jquery and lodash. In my case, I want to write a comprehensive test for an EventEmitter, which allows a user to pass a context, which gets applied to the event listener. Personally I prefer not to use context, but the feature exists. I think Jest should be able to support this kind of comprehensive testing. And while it is possible currently, it would be nice to see it more officially supported.
Thanks
The text was updated successfully, but these errors were encountered: