-
-
Notifications
You must be signed in to change notification settings - Fork 769
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
How can we override reject creating a new Error Object? #1679
Comments
That's a strawman fallacy. You know well enough that we support promise libraries, as you use them in your example. The Sinon maintainers are fully aware of how JavaScript promises work, including that
In my opinion, rejecting promises with It seems you disagree, but the only statement that you've made is that Sinon doesn't meet your expectations. If you think the default behaviour should be changed, then please post arguments for why, keeping in mind that the change has potential to affect many users. If there are solid arguments for changing the default behaviour, I can't imagine any of the maintainers of Sinon would oppose a pull request to change it. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I'd say there could be differing opinions as to whether or not a stack trace is actually useful in certain scenarios. In my particular situation, I actually don't care about the stack trace, I only care about the error message and then passing it somewhere else. So I've written code to handle both String and Error type reject reasons. However, since sinon does the String -> Error coercion for me, I cannot test the branch of my code that handles strings. As such I cannot get full coverage |
Stack trace usefulness and discussions about whether one should create error objects aside, I agree that generally const example = sinon.stub().rejects(something); should behave the same as const example = () => Promise.reject(something); Currently, this is not the case if |
I agree. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@mroderick As a solution, when stub should return a rejected (native) promise one can use
If someone decides to use |
This was really confusing to me because I assumed that stub.rejects('string) works the same as throw('string) from an async function. Workaround is to use .returns(Promise.reject('string'). After reading this thread I still don't understand why the behavior does not match throw. |
I am currently trying to weed out the small issues that's been lying dormant for ages and so I am having a look at this. @dgreenwald-ccs Whether or not an API works similar to some syntax is a bit of weird comparison. The current behavior of It's also not a bug per se, as this is the documented behavior, @mantoni. IMHO this is really a missing feature, as the current API does not allow a normal (but unfortunate) practice. The same applies to the newer Fakes API, which also has a I do think we can validate breaking the API here for some reasons
That being said, it is hard/impossible to design the API to allow all the things
We do not allow that in the simplest way of using the API, as we use a check for So my suggestion is to change the one case where we have a single argument of type string given to stub.rejects and make that just return a string as the reason. The same applies to fake.rejects. |
I want to lump the suggestion (when implemented) as a breaking change along with others for the v18 release, unless anyone objects. @mroderick what do you think? It's a quite minimal change, but breaking. AFAIK it should only need to update the stubs and fakes code. Anything else? |
Not much discussion here, but I made a PR (#2569) to implement the changes proposed. Think of it as a proposal, not necessarily the fix. In the PR I discuss how it might be just as valid to not change anything, rather just improving docs of how things work and make examples of how to use the existing methods to cover the special cases. |
I closed the PR. What we need is more/better examples how to use the API. |
@hexeberlin and I were working on solving (the now closed) #2580. We agree with the following points put forwards in the discussion above:
So, we propose to solve this by creating a new major version and making |
I don't oppose changing the API, but do keep in mind my comment above, @mroderick
I found that it wasn't possible to be 1:1 either way, so that's part of the reason why I closed my PR with the changes. Replacing one version where you need to consult the docs with another where you have to consult the docs as well was not good enough reason for changing, I thought. |
sinon/lib/sinon/default-behaviors.js
Line 176 in 4310343
I'm maintaining some code that rejects strings and this is creating issues while testing. my bluebird catches are not behaving correctly.
However using
sinon.stub().usingPromise('bluebird').rejects('Something');
ends up creating an error object with the name Something. Not expected at all.Before someone says we are only supporting native Promises, let's investigate what they do!
Hmmm, they don't create an Error class, they just toss the exception it was given like a throw.
The text was updated successfully, but these errors were encountered: