From 350178b7906359e13a9c80291a03dd637d48f328 Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Mon, 9 Mar 2020 14:12:01 +0100 Subject: [PATCH] Fix #2226: restoring non-configurable props on prototype chain --- lib/sinon/default-behaviors.js | 2 +- lib/sinon/stub.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index 96a4b416d..d7387f7a9 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -258,7 +258,7 @@ var defaultBehaviors = { Object.defineProperty(rootStub.rootObj, rootStub.propName, { value: newVal, enumerable: true, - configurable: isPropertyConfigurable(rootStub.rootObj, rootStub.propName) + configurable: rootStub.shadowsPropOnPrototype || isPropertyConfigurable(rootStub.rootObj, rootStub.propName) }); return fake; diff --git a/lib/sinon/stub.js b/lib/sinon/stub.js index 3ddca0a99..9dde8858e 100644 --- a/lib/sinon/stub.js +++ b/lib/sinon/stub.js @@ -94,11 +94,14 @@ function stub(object, property) { var func = typeof actualDescriptor.value === "function" ? actualDescriptor.value : null; var s = createStub(func); + var propIsOwn = Boolean(actualDescriptor.isOwn); + extend.nonEnum(s, { rootObj: object, propName: property, + shadowsPropOnPrototype: !propIsOwn, restore: function restore() { - if (actualDescriptor !== undefined) { + if (actualDescriptor !== undefined && propIsOwn) { Object.defineProperty(object, property, actualDescriptor); return; }