sinonjs / sinon

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

Support callable instances #2517

Closed bojavou closed 1 year ago

bojavou commented 1 year ago

Purpose (TL;DR) - mandatory

Support callable instances Closes #2516

Background (Problem in detail)

Enables testing constructors that return functions. #2516 has further description.

Solution - optional

The constructor return value seems to be checked in proxy-invoke.js. This expands the typeof check to include functions.

if (typeof returnValue !== "object" && typeof returnValue !== "function") {
  returnValue = thisValue;
}

This works for my code in a local patched version. Tests are included to verify.

How to verify - mandatory

  1. Check out this branch
  2. npm install
  3. npm test

You can verify manually by running this code from the checkout root. Output should be true.

import sinon from './pkg/sinon-esm.js'
function CallableInstance () { return () => {} }
const SpiedCallableInstance = sinon.spy(CallableInstance)
const callable = new SpiedCallableInstance()
console.log(typeof callable === 'function')
$ nano test.js
true

Checklist for author

codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (05f05ac) 96.00% compared to head (da60bbf) 96.00%.

:exclamation: Current head da60bbf differs from pull request most recent head be30ce0. Consider uploading reports for the commit be30ce0 to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #2517 +/- ## ======================================= Coverage 96.00% 96.00% ======================================= Files 40 40 Lines 1900 1900 ======================================= Hits 1824 1824 Misses 76 76 ``` | Flag | Coverage Δ | | |---|---|---| | unit | `96.00% <100.00%> (ø)` | | 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://app.codecov.io/gh/sinonjs/sinon/pull/2517?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs) | Coverage Δ | | |---|---|---| | [lib/sinon/proxy-invoke.js](https://app.codecov.io/gh/sinonjs/sinon/pull/2517?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sinonjs#diff-bGliL3Npbm9uL3Byb3h5LWludm9rZS5qcw==) | `97.91% <100.00%> (ø)` | |

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

bojavou commented 1 year ago

Thank you! I'll be glad to switch back to mainline.