sinonjs / sinon

Test spies, stubs and mocks for JavaScript.
https://sinonjs.org/
Other
9.63k stars 769 forks source link

fix throws().callsFake() precedence #2497

Closed ziluvatar closed 1 year ago

ziluvatar commented 1 year ago

Purpose (TL;DR) - mandatory

Fixes https://github.com/sinonjs/sinon/issues/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

codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 100.00% and no project coverage change.

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

:exclamation: 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 ```diff @@ 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%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs#carryforward-flags-in-the-pull-request-comment) to find out more. | [Impacted Files](https://codecov.io/gh/sinonjs/sinon/pull/2497?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) | Coverage Δ | | |---|---|---| | [lib/sinon/default-behaviors.js](https://codecov.io/gh/sinonjs/sinon/pull/2497?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs#diff-bGliL3Npbm9uL2RlZmF1bHQtYmVoYXZpb3JzLmpz) | `100.00% <100.00%> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs)

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

fatso83 commented 1 year ago

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

fatso83 commented 1 year ago

published as sinon 15.0.2