diff --git a/lib/sinon/util/core/extend.js b/lib/sinon/util/core/extend.js index 93e2d0c30..4e3d5cbac 100644 --- a/lib/sinon/util/core/extend.js +++ b/lib/sinon/util/core/extend.js @@ -73,8 +73,8 @@ function extendCommon(target, sources, doCopy) { return target; } -/** Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will - * override properties in previous sources. +/** Public: Extend target in place with all (own) properties, except 'name' when [[writable]] is false, + * from sources in-order. Thus, last source will override properties in previous sources. * * @arg {Object} target - The Object to extend * @arg {Object[]} sources - Objects to copy properties from. @@ -85,7 +85,18 @@ module.exports = function extend(target /*, sources */) { var sources = slice(arguments, 1); return extendCommon(target, sources, function copyValue(dest, source, prop) { - dest[prop] = source[prop]; + var destOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(dest, prop); + var sourceOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(source, prop); + + // If the property is 'name' but is not [[writable]], skip it + if (prop !== "name" || destOwnPropertyDescriptor.writable) { + Object.defineProperty(dest, prop, { + configurable: sourceOwnPropertyDescriptor.configurable, + enumerable: sourceOwnPropertyDescriptor.enumerable, + writable: sourceOwnPropertyDescriptor.writable, + value: sourceOwnPropertyDescriptor.value + }); + } }); };