Skip to content
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

fix throws().callsFake() precedence #2497

Merged
merged 1 commit into from Mar 12, 2023

Conversation

ziluvatar
Copy link
Contributor

@ziluvatar ziluvatar commented Mar 11, 2023

Purpose (TL;DR) - mandatory

Fixes #2496 by cleaning any previous exception* behavior when callsFake is used.

Background (Problem in detail)

Precedence among behaviors is defined by the behavior itself (cleaning other previously set behaviors) and by the order at invocation time.

Since throws behavior (exception and exceptionHandler) is the first one to run at invocation time, other behaviors can only define precedence on its own definition. That's why I modified callsFake behavior to clean throws

Solution

When callsFake is chained on a stub it cleans previous exception and exceptionHandler values which essentially invalidates any previous throws or throwsException behaviours.

Note: I think this fix should not break any current usage, because any attempt to run the stubbed function would have raised an exception already and the developer would have noticed the same I did notice: callsFake was ignored, so that, probably they applied some workaround like restoring + callsFake OR recreating the stub + callsFake.

How to verify - mandatory

Run:

const fn = sinon.stub().throws('not mocked');
fn.callsFake(() => 'mocked behavior')

console.log(fn())

You should see:

mocked behavior

Checklist for author

  • npm run lint passes
  • n/a References to standard library functions are cached.

@codecov
Copy link

codecov bot commented Mar 11, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (45be60f) 95.95% compared to head (95576e2) 95.95%.

❗ Current head 95576e2 differs from pull request most recent head 4aab223. Consider uploading reports for the commit 4aab223 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2497   +/-   ##
=======================================
  Coverage   95.95%   95.95%           
=======================================
  Files          38       38           
  Lines        1878     1880    +2     
=======================================
+ Hits         1802     1804    +2     
  Misses         76       76           
Flag Coverage Δ
unit 95.95% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
lib/sinon/default-behaviors.js 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@fatso83 fatso83 merged commit 6cbde9b into sinonjs:main Mar 12, 2023
8 checks passed
@fatso83
Copy link
Contributor

fatso83 commented Mar 12, 2023

This is great and works just like the rest. Thanks!

fatso83 pushed a commit that referenced this pull request Mar 12, 2023
This makes sure an unconditional `callsFake()` invoked on the same stub that was previously setup to throw will overwrite the previous behavior. This aligns it with the other behaviors.
@fatso83
Copy link
Contributor

fatso83 commented Mar 12, 2023

published as sinon 15.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

throws() keeps raising exception with callsFake()
2 participants