Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix(patch): fix #707, should not try to patch non configurable proper…
Browse files Browse the repository at this point in the history
…ty (#717)
  • Loading branch information
JiaLiPassion authored and mhevery committed Apr 10, 2017
1 parent 160531b commit e422fb1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export const isMix: boolean = typeof process !== 'undefined' &&

export function patchProperty(obj: any, prop: string) {
const desc = Object.getOwnPropertyDescriptor(obj, prop) || {enumerable: true, configurable: true};
// if the descriptor is not configurable
// just return
if (!desc.configurable) {
return;
}

const originalDesc = Object.getOwnPropertyDescriptor(obj, 'original' + prop);
if (!originalDesc && desc.get) {
Expand Down
21 changes: 20 additions & 1 deletion test/common/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {patchMethod, zoneSymbol} from '../../lib/common/utils';
import {patchMethod, patchProperty, zoneSymbol} from '../../lib/common/utils';

describe('utils', function() {

Expand Down Expand Up @@ -61,6 +61,25 @@ describe('utils', function() {
expect(pMethod).toBe(Type.prototype.method);
});

it('should not patch property which is not configurable', () => {
const TestType = function() {};
const originalDefineProperty = (Object as any)[zoneSymbol('defineProperty')];
if (originalDefineProperty) {
originalDefineProperty(
TestType.prototype, 'nonConfigurableProperty',
{configurable: false, writable: true, value: 'test'});
} else {
Object.defineProperty(
TestType.prototype, 'nonConfigurableProperty',
{configurable: false, writable: true, value: 'test'});
}
patchProperty(TestType.prototype, 'nonConfigurableProperty');
const desc = Object.getOwnPropertyDescriptor(TestType.prototype, 'nonConfigurableProperty');
expect(desc.writable).toBeTruthy();
expect(!desc.get).toBeTruthy();
});


it('should have a method name in the stacktrace', () => {
const fn = function someOtherName() {
throw new Error('MyError');
Expand Down

0 comments on commit e422fb1

Please sign in to comment.