Skip to content

Commit

Permalink
Issue 1936: Make the spy functions non enumerable so that printing it…
Browse files Browse the repository at this point in the history
… is more concise
  • Loading branch information
LouisBrunner committed Oct 30, 2018
1 parent 07e4994 commit ae0b95c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/sinon/spy.js
Expand Up @@ -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;
Expand Down
50 changes: 39 additions & 11 deletions lib/sinon/util/core/extend.js
Expand Up @@ -51,24 +51,15 @@ 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++) {
source = sources[i];

for (prop in source) {
if (hasOwnProperty(source, prop)) {
target[prop] = source[prop];
doCopy(target, source, prop);
}
}

Expand All @@ -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
});
});
};

0 comments on commit ae0b95c

Please sign in to comment.