From ef2ecec38c0bfd7aaeba3cca19bb9c74793b7ca2 Mon Sep 17 00:00:00 2001 From: Gyandeep Singh Date: Tue, 20 Jun 2017 02:30:15 -0500 Subject: [PATCH] Check configurable on a prop before creating (fixes #1456) (#1462) * Fix: check configurable on a prop before creating (fixes #1456) --- lib/sinon/default-behaviors.js | 15 ++++++++++++--- test/issues/issues-test.js | 21 +++++++++++++++++++++ test/stub-test.js | 13 +++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index fcd2422bc..13a8ce7b7 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -1,4 +1,7 @@ "use strict"; + +var getPropertyDescriptor = require("./util/core/get-property-descriptor"); + var slice = [].slice; var useLeftMostCallback = -1; var useRightMostCallback = -2; @@ -14,6 +17,12 @@ function throwsException(fake, error, message) { } } +function isPropertyConfigurable(obj, propName) { + var propertyDescriptor = getPropertyDescriptor(obj, propName); + + return propertyDescriptor ? propertyDescriptor.configurable : true; +} + module.exports = { callsFake: function callsFake(fake, fn) { fake.fakeFn = fn; @@ -181,7 +190,7 @@ module.exports = { Object.defineProperty(rootStub.rootObj, rootStub.propName, { get: getterFunction, - configurable: true + configurable: isPropertyConfigurable(rootStub.rootObj, rootStub.propName) }); return fake; @@ -192,7 +201,7 @@ module.exports = { Object.defineProperty(rootStub.rootObj, rootStub.propName, { // eslint-disable-line accessor-pairs set: setterFunction, - configurable: true + configurable: isPropertyConfigurable(rootStub.rootObj, rootStub.propName) }); return fake; @@ -204,7 +213,7 @@ module.exports = { Object.defineProperty(rootStub.rootObj, rootStub.propName, { value: newVal, enumerable: true, - configurable: true + configurable: isPropertyConfigurable(rootStub.rootObj, rootStub.propName) }); return fake; diff --git a/test/issues/issues-test.js b/test/issues/issues-test.js index 4bf67bd3d..6434349e1 100644 --- a/test/issues/issues-test.js +++ b/test/issues/issues-test.js @@ -2,6 +2,7 @@ var referee = require("referee"); var sinon = require("../../lib/sinon"); +var sinonSandbox = require("../../lib/sinon/sandbox"); var configureLogError = require("../../lib/sinon/util/core/log_error.js"); var assert = referee.assert; var refute = referee.refute; @@ -248,4 +249,24 @@ describe("issues", function () { }); }); }); + + if (typeof window !== "undefined") { + describe("#1456", function () { + var sandbox; + + beforeEach(function () { + sandbox = sinonSandbox.create(); + }); + + afterEach(function () { + sandbox.restore(); + }); + + it("stub window innerHeight", function () { + sandbox.stub(window, "innerHeight").value(111); + + assert.equals(window.innerHeight, 111); + }); + }); + } }); diff --git a/test/stub-test.js b/test/stub-test.js index cfc269d92..e0fd4f1de 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -2672,5 +2672,18 @@ describe("stub", function () { assert.equals(myFunc.prop, "rawString"); }); + + it("allows stubbing object props with configurable false", function () { + var myObj = {}; + Object.defineProperty(myObj, "prop", { + configurable: false, + enumerable: true, + writable: true, + value: "static" + }); + + createStub(myObj, "prop").value("newString"); + assert.equals(myObj.prop, "newString"); + }); }); });