From 50a241c3e37221d0850edd17bf599fbb95b30b64 Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Sun, 26 Mar 2023 12:39:37 +0200 Subject: [PATCH] internal: extract sinon type checking into own module --- lib/sinon/create-stub-instance.js | 5 +++-- lib/sinon/stub.js | 3 +++ lib/sinon/util/core/sinon-type.js | 22 ++++++++++++++++++++++ lib/sinon/util/core/wrap-method.js | 3 ++- test/create-stub-instance-test.js | 4 ++-- test/stub-test.js | 2 +- 6 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 lib/sinon/util/core/sinon-type.js diff --git a/lib/sinon/create-stub-instance.js b/lib/sinon/create-stub-instance.js index 23baf4fb0..e4fd4148e 100644 --- a/lib/sinon/create-stub-instance.js +++ b/lib/sinon/create-stub-instance.js @@ -1,10 +1,11 @@ "use strict"; const stub = require("./stub"); +const sinonType = require("./util/core/sinon-type"); const forEach = require("@sinonjs/commons").prototypes.array.forEach; function isStub(value) { - return value && value.throws && value.returns; + return sinonType.get(value) === "stub"; } module.exports = function createStubInstance(constructor, overrides) { @@ -13,7 +14,7 @@ module.exports = function createStubInstance(constructor, overrides) { } const stubInstance = Object.create(constructor.prototype); - stubInstance[Symbol.for("SinonType")] = "StubInstance"; + sinonType.set(stubInstance, "stub-instance"); const stubbedObject = stub(stubInstance); diff --git a/lib/sinon/stub.js b/lib/sinon/stub.js index 0edc88d5a..d521b6ce8 100644 --- a/lib/sinon/stub.js +++ b/lib/sinon/stub.js @@ -12,6 +12,7 @@ var spy = require("./spy"); var extend = require("./util/core/extend"); var getPropertyDescriptor = require("./util/core/get-property-descriptor"); var isEsModule = require("./util/core/is-es-module"); +var sinonType = require("./util/core/sinon-type"); var wrapMethod = require("./util/core/wrap-method"); var throwOnFalsyObject = require("./throw-on-falsy-object"); var valueToString = require("@sinonjs/commons").valueToString; @@ -58,6 +59,8 @@ function createStub(originalFunc) { id: `stub#${uuid++}`, }); + sinonType.set(proxy, "stub"); + return proxy; } diff --git a/lib/sinon/util/core/sinon-type.js b/lib/sinon/util/core/sinon-type.js new file mode 100644 index 000000000..3a674a2be --- /dev/null +++ b/lib/sinon/util/core/sinon-type.js @@ -0,0 +1,22 @@ +"use strict"; + +const sinonTypeSymbolProperty = Symbol("SinonType"); + +module.exports = { + /** + * Set the type of a Sinon object to make it possible to identify it later at runtime + * + * @param {object|Function} object object/function to set the type on + * @param {string} type the named type of the object/function + */ + set(object, type) { + Object.defineProperty(object, sinonTypeSymbolProperty, { + value: type, + configurable: false, + enumerable: false, + }); + }, + get(object) { + return object && object[sinonTypeSymbolProperty]; + }, +}; diff --git a/lib/sinon/util/core/wrap-method.js b/lib/sinon/util/core/wrap-method.js index 99ddd47a2..92036c247 100644 --- a/lib/sinon/util/core/wrap-method.js +++ b/lib/sinon/util/core/wrap-method.js @@ -4,6 +4,7 @@ const noop = () => {}; var getPropertyDescriptor = require("./get-property-descriptor"); var extend = require("./extend"); +const sinonType = require("./sinon-type"); var hasOwnProperty = require("@sinonjs/commons").prototypes.object.hasOwnProperty; var valueToString = require("@sinonjs/commons").valueToString; @@ -232,7 +233,7 @@ module.exports = function wrapMethod(object, property, method) { } } } - if (object[Symbol.for("SinonType")] === "StubInstance") { + if (sinonType.get(object) === "stub-instance") { // this is simply to avoid errors after restoring if something should // traverse the object in a cleanup phase, ref #2477 object[property] = noop; diff --git a/test/create-stub-instance-test.js b/test/create-stub-instance-test.js index c7db51ce9..6d08361e9 100644 --- a/test/create-stub-instance-test.js +++ b/test/create-stub-instance-test.js @@ -2,7 +2,7 @@ var referee = require("@sinonjs/referee"); var createStub = require("../lib/sinon/stub"); -var createStubInstance = require("../lib/sinon/create-stub-instance") +var createStubInstance = require("../lib/sinon/create-stub-instance"); var assert = referee.assert; var refute = referee.refute; @@ -155,4 +155,4 @@ describe("createStubInstance", function () { { message: "Cannot stub foo. Property does not exist!" } ); }); -}) +}); diff --git a/test/stub-test.js b/test/stub-test.js index 6524a3822..398115185 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -2,7 +2,7 @@ var referee = require("@sinonjs/referee"); var createStub = require("../lib/sinon/stub"); -var createStubInstance = require("../lib/sinon/create-stub-instance") +var createStubInstance = require("../lib/sinon/create-stub-instance"); var createSpy = require("../lib/sinon/spy"); var createProxy = require("../lib/sinon/proxy"); var match = require("@sinonjs/samsam").createMatcher;