diff --git a/src/imitate/overwrite-children/copy-props.js b/src/imitate/overwrite-children/copy-props.js index 75d7764..08182ed 100644 --- a/src/imitate/overwrite-children/copy-props.js +++ b/src/imitate/overwrite-children/copy-props.js @@ -6,21 +6,24 @@ const MAX_GETTER_DEPTH = 3 let getterDepth = 0 export default (target, props, visitor, original) => { - Object.defineProperties(target, _.transform(props, (acc, descriptor, name) => { + const descriptors = {} + Reflect.ownKeys(props).forEach(name => { + const descriptor = props[name] if (propOnTargetAndNotWritable(target, name, descriptor)) { if (name === 'prototype') { // Functions' prototype is not configurable but is assignable: target.prototype = valueFor(name, descriptor, original, visitor) } } else { - acc[name] = { + descriptors[name] = { configurable: true, writable: true, value: valueFor(name, descriptor, original, visitor), enumerable: descriptor.enumerable } } - })) + }) + Object.defineProperties(target, descriptors) } const propOnTargetAndNotWritable = (target, name, originalDescriptor) => { diff --git a/src/imitate/overwrite-children/gather-props.js b/src/imitate/overwrite-children/gather-props.js index 862b72d..fa9c368 100644 --- a/src/imitate/overwrite-children/gather-props.js +++ b/src/imitate/overwrite-children/gather-props.js @@ -4,7 +4,7 @@ import isNativePrototype from './is-native-prototype' export default function gatherProps (thing) { const props = {} while (isFakeable(thing) && !isNativePrototype(thing)) { - Object.getOwnPropertyNames(thing).forEach((propName) => { + Reflect.ownKeys(thing).forEach((propName) => { if (!props[propName] && propName !== 'constructor') { props[propName] = Object.getOwnPropertyDescriptor(thing, propName) } diff --git a/src/imitate/overwrite-children/index.js b/src/imitate/overwrite-children/index.js index de37e45..7de9fd9 100644 --- a/src/imitate/overwrite-children/index.js +++ b/src/imitate/overwrite-children/index.js @@ -13,8 +13,9 @@ export default (original, target, overwriteChild) => { target.push(overwriteChild(item, `[${index}]`)) ) } else { - copyProps(target, gatherProps(original), (name, originalValue) => - chainPrototype(original, target, name, originalValue, - overwriteChild(originalValue, `.${name}`)), original) + copyProps(target, gatherProps(original), (name, originalValue) => { + const path = typeof name === 'symbol' ? `[${String(name)}]` : `.${name}` + return chainPrototype(original, target, name, originalValue, overwriteChild(originalValue, path)) + }, original) } } diff --git a/test/safe/imitate.test.js b/test/safe/imitate.test.js index 01b90a4..6b5a81b 100644 --- a/test/safe/imitate.test.js +++ b/test/safe/imitate.test.js @@ -89,6 +89,23 @@ module.exports = { assert._isEqual(td.explain(result.shirt).name, 'pants.shirt') assert._isEqual(td.explain(result.shirt.tie).name, 'pants.shirt.tie') }, + 'imitates own and inherited symbol methods with the same description' () { + const own = Symbol('read') + const inherited = Symbol('read') + class Reader { + [inherited] () {} + } + const original = new Reader() + original[own] = () => {} + + const result = td.imitate(original) + + assert.strictEqual(td.explain(result[own]).isTestDouble, true) + assert.strictEqual(td.explain(result[inherited]).isTestDouble, true) + assert.strictEqual(td.explain(result[own]).name, '[Symbol(read)]') + assert.notStrictEqual(result[own], result[inherited]) + assert.strictEqual(Object.getOwnPropertyDescriptor(result, inherited).enumerable, false) + }, 'skips over generator functions, even their custom properties' () { // This is currently unsupported, expect to kill this test someday if (!ES_SUPPORT.GENERATORS) return