Skip to content

Commit a79ccae

Browse files
author
bojavou
authoredJun 20, 2023
Support callable instances (#2517)
* Support callable instances * Clean prettier lint --------- Co-authored-by: - <->
1 parent d220c99 commit a79ccae

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed
 

‎lib/sinon/proxy-invoke.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ module.exports = function invoke(func, thisValue, args) {
4343
concat([thisValue], args)
4444
))();
4545

46-
if (typeof returnValue !== "object") {
46+
if (
47+
typeof returnValue !== "object" &&
48+
typeof returnValue !== "function"
49+
) {
4750
returnValue = thisValue;
4851
}
4952
} else {

‎test/proxy-call-test.js

+42
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,48 @@ describe("sinonSpy.call", function () {
12461246
});
12471247
});
12481248

1249+
describe("constructor return", function () {
1250+
it("preserves returned object", function () {
1251+
const customReturn = {};
1252+
function CustomConstructor() {
1253+
return customReturn;
1254+
}
1255+
const SpiedCustomConstructor = sinonSpy(CustomConstructor);
1256+
const myInstance = new SpiedCustomConstructor();
1257+
1258+
assert(myInstance === customReturn);
1259+
});
1260+
1261+
it("allows explicit returned object", function () {
1262+
const StubConstructor = sinonStub();
1263+
const customReturn = {};
1264+
StubConstructor.returns(customReturn);
1265+
const myInstance = new StubConstructor();
1266+
1267+
assert(myInstance === customReturn);
1268+
});
1269+
1270+
it("preserves returned function", function () {
1271+
function customReturn() {} // eslint-disable-line no-empty-function
1272+
function CustomConstructor() {
1273+
return customReturn;
1274+
}
1275+
const SpiedCustomConstructor = sinonSpy(CustomConstructor);
1276+
const myInstance = new SpiedCustomConstructor();
1277+
1278+
assert(myInstance === customReturn);
1279+
});
1280+
1281+
it("allows explicit returned function", function () {
1282+
const StubConstructor = sinonStub();
1283+
function customReturn() {} // eslint-disable-line no-empty-function
1284+
StubConstructor.returns(customReturn);
1285+
const myInstance = new StubConstructor();
1286+
1287+
assert(myInstance === customReturn);
1288+
});
1289+
});
1290+
12491291
describe("functions", function () {
12501292
it("throws if spying on non-existent property", function () {
12511293
const myObj = {};

0 commit comments

Comments
 (0)
Please sign in to comment.