From bb18cd35c271a10b7b0b45eaebd1b388ec12cc99 Mon Sep 17 00:00:00 2001 From: YushuoSun <135411736+yushuosun@users.noreply.github.com> Date: Sun, 29 Nov 2026 20:39:25 +0800 Subject: [PATCH] Imitate symbol-keyed methods on objects and their prototypes Fixes #296. --- src/imitate/overwrite-children/copy-props.js | 9 ++++++--- src/imitate/overwrite-children/gather-props.js | 2 +- src/imitate/overwrite-children/index.js | 7 ++++--- test/safe/imitate.test.js | 17 +++++++++++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/imitate/overwrite-children/copy-props.js b/src/imitate/overwrite-children/copy-props.js index 75d7764b..08182ed1 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 862b72dd..fa9c3688 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 de37e45f..7de9fd9b 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 01b90a49..6b5a81b8 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