Skip to content

Commit 737df04

Browse files
committed
Fix bug caused by things with undefined prototypes
A user was doing td.replace('aws-sdk'), which is massive and contains a lot of really screwy prototype manipulation that apparently has at least one type with an undefined in the prototype chain of one of its functions. This guards against that. The test isn't perfect because I wasn't even able to reproduce the same prototype chain nonsense
1 parent 8fba8b9 commit 737df04

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/imitate/overwrite-children/gather-props.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import _ from '../../wrap/lodash'
22

3+
import isPrimitiveLike from './is-primitive-like'
4+
35
export default (thing) => {
46
const originalThing = thing
57
const props = {}
68

7-
while (!isNativePrototype(thing)) {
9+
while (!isPrimitiveLike(thing) && !isNativePrototype(thing)) {
810
Object.getOwnPropertyNames(thing).forEach((propName) => {
911
if (!props[propName] && propName !== 'constructor') {
1012
props[propName] = Object.getOwnPropertyDescriptor(thing, propName)

test/unit/imitate/overwrite-children/gather-props.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ module.exports = {
106106

107107
assert.deepEqual(_.keys(result), ['foo', 'bar'])
108108
},
109+
'is primitive-y': () => {
110+
assert.deepEqual(subject(null), {})
111+
assert.deepEqual(subject(undefined), {})
112+
assert.deepEqual(subject(false), {})
113+
assert.deepEqual(subject(true), {})
114+
assert.deepEqual(subject(5), {})
115+
},
109116
'extending a native type': () => {
110117
const Thing = function () {}
111118
Thing.prototype = Object.create(Map.prototype)

test/unit/imitate/overwrite-children/index.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ module.exports = {
99

1010
td.when(isPrimitiveLike(td.matchers.anything())).thenReturn(false)
1111
},
12-
'thing is primitive-like (then do nothing)': () => {
13-
const target = {}
14-
td.when(isPrimitiveLike(target)).thenReturn(true)
12+
'target is primitive-like (then do nothing)': () => {
13+
td.when(isPrimitiveLike('some target')).thenReturn(true)
1514

16-
subject('some original', target, () => { throw new Error('do not fire!') })
15+
subject('some original', 'some target', () => { throw new Error('do not fire!') })
1716

18-
assert.deepEqual(target, {})
1917
td.verify(gatherProps(), {ignoreExtraArgs: true, times: 0})
2018
td.verify(copyProps(), {ignoreExtraArgs: true, times: 0})
2119
td.verify(chainPrototype(), {ignoreExtraArgs: true, times: 0})

0 commit comments

Comments
 (0)