Skip to content

Commit 2ec2955

Browse files
authored
fix: Preserve function prototype when filling (#1907)
1 parent bd35d73 commit 2ec2955

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/utils/src/object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function fill(source: { [key: string]: any }, name: string, replacement:
6767
// otherwise it'll throw "TypeError: Object.defineProperties called on non-object"
6868
// tslint:disable-next-line:strict-type-predicates
6969
if (typeof wrapped === 'function') {
70-
wrapped.prototype = {};
70+
wrapped.prototype = wrapped.prototype || {};
7171
Object.defineProperties(wrapped, {
7272
__sentry__: {
7373
enumerable: false,

packages/utils/test/object.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,21 @@ describe('fill()', () => {
340340
expect(source.foo.__sentry_original__).toBe(source.foo);
341341
expect(source.foo.__sentry_wrapped__).toBe(source.foo);
342342
});
343+
344+
test('should preserve functions prototype if one exists', () => {
345+
const source = {
346+
foo: (): number => 42,
347+
};
348+
const bar = {};
349+
source.foo.prototype = bar;
350+
const name = 'foo';
351+
const replacement = cb => cb;
352+
353+
fill(source, name, replacement);
354+
355+
// But should be accessible directly
356+
expect(source.foo.prototype).toBe(bar);
357+
});
343358
});
344359

345360
describe('urlEncode()', () => {

0 commit comments

Comments
 (0)