fix: make return/throw behavior setters fully mutually exclusive - #2759
Open
rajanpanth wants to merge 1 commit into
Open
fix: make return/throw behavior setters fully mutually exclusive#2759rajanpanth wants to merge 1 commit into
rajanpanth wants to merge 1 commit into
Conversation
Each of returns/returnsArg/throwsArg/returnsThis/callsFake/resolves/ resolvesArg/rejects/resolvesThis/callThrough/callThroughWithNew only cleared the specific fields its author happened to think of, rather than the full set behavior.js#invoke checks. So e.g. returnsArg followed by throwsArg, callsFake, returnsThis, or resolves left returnArgAt in place, which invoke() checks before any of those -- silently keeping the earlier behavior instead of being overridden. d36e921 fixed this for the returns-after-returnsArg pair specifically; this generalizes that fix into a shared resetBehavior() called by every return/throw setter, so a future setter can't reintroduce the same class of bug by omission. Deliberately excludes the callsArg/yields callback-invocation fields from that reset: yielding a callback is a separate, additive side effect meant to combine with any of these behaviors regardless of call order (see the existing "returnsArg takes precedent over yielded return value" tests), not another mutually-exclusive choice in this set. callThrough/ callThroughWithNew still clear the callback config too, since "call the real method" is meant to replace all stub customization -- this also fixes callThroughWithNew never having cleared returnArgAt/ returnThis/fakeFn/resolve*/reject/returnValue, unlike callThrough.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Each of
returns/returnsArg/throwsArg/returnsThis/callsFake/resolves/resolvesArg/rejects/resolvesThis/callThrough/callThroughWithNewonly clears the specific fields its author happened to think of, rather than the full setbehavior.js#invokechecks. So e.g.returnsArgfollowed bythrowsArg,callsFake,returnsThis, orresolvesleavesreturnArgAtin place, whichinvoke()checks before any of those — silently keeping the earlier behavior instead of being overridden:#d36e921 fixed this for the
returns-after-returnsArgpair specifically, but the same bug shape remained for every other combination.Changes
src/sinon/default-behaviors.js: generalizes that fix into a sharedresetBehavior(), called by every return/throw setter before it sets its own field(s). This is the same exhaustive-reset patterncallThroughalready used — I just made every setter in this family use it consistently instead of each remembering to clear a handful of fields ad hoc.callsArg/yieldscallback-invocation fields from this reset: yielding a callback is a separate, additive side effect meant to combine with any of these return/throw behaviors regardless of call order (see the existing "returnsArg takes precedent over yielded return value" tests) — not another mutually-exclusive choice in this set. Left thecallsArg/yieldsfamily of setters untouched.callThrough/callThroughWithNewstill additionally clear the callback config too, since "call the real method" is meant to replace all stub customization. This also fixescallThroughWithNewnever having clearedreturnArgAt/returnThis/fakeFn/resolve*/reject/returnValue, unlikecallThrough(which already cleared everything).test/src/stub-test.js: 6 new "is superseded by X" tests alongside the existing ones, covering the previously-broken combinations.Testing
Sandbox > warns of potential leak when many fakes are created, a 2000ms timeout) is pre-existing and unrelated — confirmed viagit stash, it fails identically without this change.eslint --max-warnings 0clean.