-
-
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 support for 'using' keyword to dispose mocks/spies #14294
Comments
Could you elaborate on what you would expect from |
Something like this is super exciting, but I also don't know if we can really attach semantics here without breaking somebody's workflow |
I would expect a full clean-up since the variable will be available only for the block: spyOn(...) {
// ...
[Symbol.dispose]() {
this.mockReset()
}
} I assume it is also relevant for 2#, 3#
Why would it break? By using |
Thanks. I think it is worth to think if But I can’t see it working with the Jest Object which is the return value of (I talk about fake timers, because In the Probably it could work with {
using spiedMethod = jest.spyOn(someObj, 'someMethod')
// do something
// calls `spiedMethod.mockRestore()`
} Not sure if that makes sense with |
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. |
@mrazauskas @SimenB - Just making sure, is this something you plan to adopt? Should I consider contributing to this :) |
What is In general it sounds useful. At this moment the concrete use cases are somewhat |
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. |
PR welcome, yeah 👍 |
Example in the wild: apollographql/apollo-client#11177 |
@tzachbon are you still up for sending a PR here? 🙂 |
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
Add support for the 'using' keyword, recently introduced in TypeScript 5.2, to handle the disposal of mock resources in Jest. This keyword provides a simple and efficient way to manage resources, and it could greatly enhance the cleanup process of mocks after each test run.
Motivation
The 'using' keyword in TypeScript 5.2 is designed to automatically dispose of anything with a
Symbol.dispose
function when it leaves scope. Given its utility in managing resources such as file handles and database connections, applying this feature to Jest's mock resources would provide a cleaner and more efficient way to ensure that mocks are properly cleaned up after each test run. This could reduce the risk of memory leaks or other unexpected behavior, enhancing the reliability and performance of tests.Example
These are just some examples that I would imagine I will use if it will be natively supported from the returned value of "spyOn" or "mock"
Pitch
This feature aligns with the latest practices in TypeScript (and soon JavaScript), providing a more modern and efficient way to manage and dispose of mock resources. Given that Jest is a core tool for testing in the JavaScript ecosystem, it's crucial that it continues to evolve and incorporate these advancements. This feature would not just be a nice-to-have, but rather a significant improvement to Jest's handling of mock resources, making it a valuable addition to the core platform.
The text was updated successfully, but these errors were encountered: