-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
mockResolvedValue / mockRejectedValue should returns a real Promise #6645
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I wonder if this is due to #2549? |
Hello, maybe it's also related to this issue. The error I get when using
I have an async utility to be able to handle individual errors in a Promise.all. /**
* Return both failed and successful promises
* @param {Array.<Promise>} promises
*/
function promiseAllResults (promises) {
return Promise.all(promises.map((promise) => promise.catch((err) => err)))
} If I use a "real promise" as a mock, it works. (but then I can not spy) |
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. |
This issue isn't fixed for 4 years (?!) and it's still causing problems like this (#13206)? Please fix asap or remove that sugar-function. |
I agree with @mhombach I struggled with tests for some time to end up discovering this issue. Would be good to fix it or remove it completely. |
@SimenB hey :) I tested the This is tested with a brandnew angular project using Jest I also tested with without the Any idea? |
@mhombach that's probably a separate issue - the reproduction from the OP passes now. Could you open up a new issue with a minimal reproduction? |
@SimenB I created a repro-repo (https://github.com/mhombach/jest_repro_6645) can you have a look? The thing is that if I test const resolvedPromise = jest.fn().mockResolvedValue('hello')();
const rejectedPromise = jest.fn().mockRejectedValue('hello')();
expect(resolvedPromise).toBeInstanceOf(Promise);
expect(rejectedPromise).toBeInstanceOf(Promise); the unit test seems to not fail hard but just be "not passing" in some way: Please use PS: Use the file |
You need to await at least the rejected promise, or it's an unhandled rejection. Can you open up a new issue? This one is resolved and easy to lose track of 🙂 |
@SimenB I feel there is a misunderstanding here. Just to make it even more precise: const resolvedPromise = jest.fn().mockResolvedValue('hello')();
const rejectedPromise = jest.fn().mockRejectedValue('hello')();
expect(resolvedPromise).toBeInstanceOf(Promise);
expect(rejectedPromise).toBeInstanceOf(Promise); The same code is what I am testing locally. And on my machine it's silently failing. Which makes me believe, that it's also failing silently on Github-CI. Which means, the test is flawed and you cannot ensure that the functionality of your fix is working as expected. Again, if this is some huge misunderstanding, correct me and I will gladly open a new issue :) |
You're missing the |
This is 100% the code you added in the fix (except the one commented line which I added. |
Same thing - Put |
@SimenB This means if I negate the 2 tests you have there by adding a your 2 Adding 2 console-logs there you can see that the testing of the "instance of" is not working at all: ... Which was the hole purpose of this |
Please open up a new issue with a minimal reproduction |
🐛 Bug Report
mockResolvedValue
andmockRejectedValue
should return a real Promise testable withtoBeInstanceOf
but they don't.To Reproduce
Expected behavior
It should pass
The behaviour should be the same as using
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: