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

Testing function context ("this") in Mock functions #11235

Closed
matthias-ccri opened this issue Mar 23, 2021 · 5 comments · Fixed by #12601
Closed

Testing function context ("this") in Mock functions #11235

matthias-ccri opened this issue Mar 23, 2021 · 5 comments · Fixed by #12601

Comments

@matthias-ccri
Copy link
Contributor

🚀 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 or bind:

const ctx = {};
mockFn.call(ctx, arg1, arg2);

It is currently possible to test the context with mock.instances, but this doesn't seem to be the intended purpose. According to the docs mock.instances seems class-related:

An array that contains all the object instances that have been instantiated from this mock function using new.

In fact, there is an open issue (#7070) discussing how to "fix" instances to only be the return value of new 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:

const spy = jest.fn();
const context = {};
spy.call(context);

// a new `toHaveBeenCalledWithContext` expectation
expect(spy).toHaveBeenCalledWithContext(context);
// or a new `mock.contexts` property similar to `mock.instances`
expect(spy.mock.contexts[0]).toBe(context);

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

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Mar 23, 2022
@SimenB
Copy link
Member

SimenB commented Mar 24, 2022

I don't think we should add matchers for this. but adding it as a context like your seconds suggestion sounds good to me. Wanna send a PR? 🙂

@github-actions github-actions bot removed the Stale label Mar 24, 2022
matthias-ccri added a commit to matthias-ccri/jest that referenced this issue Mar 24, 2022
@matthias-ccri
Copy link
Contributor Author

#12601

@SimenB SimenB linked a pull request Mar 25, 2022 that will close this issue
@SimenB
Copy link
Member

SimenB commented Apr 5, 2022

@github-actions
Copy link

github-actions bot commented May 6, 2022

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants