Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose (TL;DR) - mandatory
Fix #1679 by changing the Stub and Fake API to not wrap strings in Error
Background (Problem in detail)
In #1679 it was raised how stub.rejects("error") does not reject the promise with the passed in reason, but chooses to wrap it in an error. Some people found that behavior surprising that and had objections to that API design.
@mantoni chimed in to say that he thought stub.rejects should behave in line with
Promise.reject
.I made the point that while
stub.rejects(string)
might be surprising to someone comparing it to thePromise.reject
API _it is totally consistent with the current behavior ofstub.throws(string)
and that we should keep the two aligned.Solution
This is one solution to the issue: it changes the
rejects
andthrows
API's of Stub and Fake to not treat the single-argument string in a special matter. This will potentially break a lot of existing code, but at least keeps consistency across the API's for throwing sync and async.There are other takes, one being to simply keep things as they are and document in the docs how to these are utility methods to achieve the most normal flow (which is throwing an error sync and async), and simply update the docs (both JSDoc and homepage docs) to point to other methods for achieving more specialized cases.
For instance, if you want to throw a string using the API that is today (before this change), you can just create a fake or stub that does that:
Same goes for Promises / throwing async
I think this is just as valid an approach: making people aware that
the basic building blocks are there. That's what I like about the
Fake API: fewer methods in that API to rather create explicit
behavior that creates less misconceptions about what something is doing.
How to verify - mandatory
npm install
Checklist for author
npm run lint
passes