Skip to content

Commit

Permalink
Support callable instances (#2517)
Browse files Browse the repository at this point in the history
* Support callable instances

* Clean prettier lint

---------

Co-authored-by: - <->
  • Loading branch information
bojavou committed Jun 20, 2023
1 parent d220c99 commit a79ccae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/sinon/proxy-invoke.js
Expand Up @@ -43,7 +43,10 @@ module.exports = function invoke(func, thisValue, args) {
concat([thisValue], args)
))();

if (typeof returnValue !== "object") {
if (
typeof returnValue !== "object" &&
typeof returnValue !== "function"
) {
returnValue = thisValue;
}
} else {
Expand Down
42 changes: 42 additions & 0 deletions test/proxy-call-test.js
Expand Up @@ -1246,6 +1246,48 @@ describe("sinonSpy.call", function () {
});
});

describe("constructor return", function () {
it("preserves returned object", function () {
const customReturn = {};
function CustomConstructor() {
return customReturn;
}
const SpiedCustomConstructor = sinonSpy(CustomConstructor);
const myInstance = new SpiedCustomConstructor();

assert(myInstance === customReturn);
});

it("allows explicit returned object", function () {
const StubConstructor = sinonStub();
const customReturn = {};
StubConstructor.returns(customReturn);
const myInstance = new StubConstructor();

assert(myInstance === customReturn);
});

it("preserves returned function", function () {
function customReturn() {} // eslint-disable-line no-empty-function
function CustomConstructor() {
return customReturn;
}
const SpiedCustomConstructor = sinonSpy(CustomConstructor);
const myInstance = new SpiedCustomConstructor();

assert(myInstance === customReturn);
});

it("allows explicit returned function", function () {
const StubConstructor = sinonStub();
function customReturn() {} // eslint-disable-line no-empty-function
StubConstructor.returns(customReturn);
const myInstance = new StubConstructor();

assert(myInstance === customReturn);
});
});

describe("functions", function () {
it("throws if spying on non-existent property", function () {
const myObj = {};
Expand Down

0 comments on commit a79ccae

Please sign in to comment.