-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Spied-on functions that return Promises change their mock return values when the Promise resolves #3634
Comments
Start a new pull request in StackBlitz Codeflow. |
This behavior is different from jest's, but it is expected. Not sure exactly what you want to document, but feel free to open a PR. |
@sheremet-va Is there a way for a developer to inspect the original promise? I previously found it helpful to be able to inspect the original |
Eh. The use case is because I needed to check whether or not a If that's the case, maybe it isn't necessary to preserve the original. That said, an update to the |
I don't know. Maybe semantically it's better to not resolve a promise because this is what the actual function return value. So I would consider it a bug. To fix this, it's better to open a separate issue in tinyspy. |
I feel like you could argue either way. Originally I was a little upset with the implementation. But in the long run, this implementation could be considered advantageous -- as long as it's clearly documented. The disadvantage of leaving returned The current way Vitest works simplified my codebase for the aforementioned reason. (I don't have to inspect whether or not the |
We now document this difference in Vitest documentation so I think it's safe to close this issue. |
Thanks for keeping the current implementation. 🙏🏾 I like what Vitest does so much better. |
Describe the bug
If a spied-upon function returns a promise, the
.mock.results[...].value
item starts off as a promise, but when the promise resolves it becomes the resolved item. This behavior doesn't seem to be documented anywhere.I found this while migrating an existing test suite from jest, and I know this might not be the most desirable test pattern (I will simply change it to not expect this), but I found this behavior pretty surprising.
It looks like it might have been introduced in #2835.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-mk9jek?file=test%2Fbasic.test.ts
Note that you can add any amount of indirection you want here, and it'll behave the same way. So the issue isn't that I'm calling
bar
and it's spied upon - I originally discovered this issue in a test that spied onbar
, called a function that calledbar
, and examined the mock's result to make sure it was a promise (and then awaited it to examine the result, etc.).System Info
The text was updated successfully, but these errors were encountered: