-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
Add clearFields function to put logic clearing common fields in one place #1525
Conversation
Seems like a good idea, but I have to admit I don't know the internals well enough to decide. |
This means that |
Oh, I didn't read carefully. You explicitly mentioned the |
The basic combination logic here strikes me as extremely fragile and interconnected. Wouldn't this be one of the examples where refactoring to polymorphism actually makes proper sense? This is of course a bigger refactoring, but could help simplify all the conditionals we would need to handle all the combinations. |
Do you mean refactoring Sinon to use polymorphism, or do you mean that @noisecapella's code should be refactored to use polymorphism? |
I just meant the internal behaviour code of Sinon. Or at least some way of splitting the logic up to make it manageable on a per-behavior basis. |
I don't think that the code in this PR makes it any less clear what is going on in the codebase. However, the more interesting thing that @noisecapella shone a light on, is that some operations with stubs make absolutely no sense, yet are still supported. stub.throws("TypeError");
stub.returns(3);
stub.resolves('some value');
stub.rejects(new Error('some error')); These are contradictory statements, that silently change the stub's behaviour. Considering that many test scenarios have nested test setups, silently changing behaviour can lead to un-anticipated situations that might cause people's tests to pass when they really should not. I think we have an opportunity to significantly improve Sinon by making non-sensical scenarios unsupported. Perhaps we can go even further and also make a stub's behaviour (almost) immutable. By making it throw an stub.throws("TypeError");
stub.returns(3);
// => throws new Error('A stub cannot both throw and return a value');
stub.resolves('some value');
stub.rejects(new Error('some error'));
// => throws new Error('A stub cannot both resolve and reject a promise'); |
Yes, I agree that some combinations should trigger Sinon exceptions. How about certain orders of behaviors? For example
|
At my workplace we use a pattern like this:
The @mantoni That makes sense, I misunderstood |
I'm not sure if there's a consensus here so I'm going to close this and open two more specific pull requests: one for the missing |
Note: Please let me know if this solution is desirable behavior. I haven't yet added unit tests but I'm happy to add them if this PR implementation fits with the goals of the project.
Purpose (TL;DR) - mandatory
This PR adds a function
clearFields
to centralize the functionality resetting stub behavior if multiple behavior functions are called. This PR also usesclearFields
on other behavior functions likecallsFake
.Background (Problem in detail) - optional
The problem deals with the behavior of this situation:
returns
resets certain fields includingexception
andexceptionCreator
, effectively overriding the previous behavior.I originally created #1511 which added the field
exceptionCreator
, and I noticed today that in #1517 it doesn't resetexceptionCreator
. Also, my coworker told me thatcallsFake
doesn't have the same behavior resetting behavior thatreturns
does.Solution - optional
I added the
clearFields
function which clears every field I could find referenced fordefault-behaviors.js
, except forusingPromise
since I wasn't sure if that should be reset or not.There are a few extra behavior functions which clear fields which didn't clear fields before. The
yield
* functions were left alone because there were tests asserting that they didn't overridereturns
, for example this one.How to verify - mandatory
Run
npm test
Checklist for author
npm run lint
passes