From ae0b95c49ba1a1994d10d9f78313f40d3cabffea Mon Sep 17 00:00:00 2001 From: Louis Brunner Date: Sun, 28 Oct 2018 18:52:43 +0000 Subject: [PATCH] Issue 1936: Make the spy functions non enumerable so that printing it is more concise --- lib/sinon/spy.js | 4 +-- lib/sinon/util/core/extend.js | 50 +++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/sinon/spy.js b/lib/sinon/spy.js index 9edbc5e30..56372c67f 100644 --- a/lib/sinon/spy.js +++ b/lib/sinon/spy.js @@ -217,9 +217,9 @@ var spyApi = { var length = spyLength || funk.length; var proxy = createProxy(funk, length); - extend(proxy, spy); + extend.nonEnum(proxy, spy); delete proxy.create; - extend(proxy, funk); + extend.nonEnum(proxy, funk); proxy.resetHistory(); proxy.prototype = funk.prototype; diff --git a/lib/sinon/util/core/extend.js b/lib/sinon/util/core/extend.js index b400786b8..93e2d0c30 100644 --- a/lib/sinon/util/core/extend.js +++ b/lib/sinon/util/core/extend.js @@ -51,16 +51,7 @@ var hasDontEnumBug = (function() { return join(result, "") !== "0123456789"; })(); -/* Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will - * override properties in previous sources. - * - * target - The Object to extend - * sources - Objects to copy properties from. - * - * Returns the extended target - */ -module.exports = function extend(target /*, sources */) { - var sources = slice(arguments, 1); +function extendCommon(target, sources, doCopy) { var source, i, prop; for (i = 0; i < sources.length; i++) { @@ -68,7 +59,7 @@ module.exports = function extend(target /*, sources */) { for (prop in source) { if (hasOwnProperty(source, prop)) { - target[prop] = source[prop]; + doCopy(target, source, prop); } } @@ -80,4 +71,41 @@ module.exports = function extend(target /*, sources */) { } return target; +} + +/** Public: Extend target in place with all (own) properties 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. + * + * @returns {Object} the extended target + */ +module.exports = function extend(target /*, sources */) { + var sources = slice(arguments, 1); + + return extendCommon(target, sources, function copyValue(dest, source, prop) { + dest[prop] = source[prop]; + }); +}; + +/** Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will + * override properties in previous sources. Define the properties as non enumerable. + * + * @arg {Object} target - The Object to extend + * @arg {Object[]} sources - Objects to copy properties from. + * + * @returns {Object} the extended target + */ +module.exports.nonEnum = function extendNonEnum(target /*, sources */) { + var sources = slice(arguments, 1); + + return extendCommon(target, sources, function copyProperty(dest, source, prop) { + Object.defineProperty(dest, prop, { + value: source[prop], + enumerable: false, + configurable: true, + writable: true + }); + }); };