Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/zone.js/lib/jasmine/jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ Zone.__load_patch('jasmine', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
return clock;
};
}

// monkey patch createSpyObj to make properties enumerable to true
if (!(jasmine as any)[Zone.__symbol__('createSpyObj')]) {
const originalCreateSpyObj = jasmine.createSpyObj;
(jasmine as any)[Zone.__symbol__('createSpyObj')] = originalCreateSpyObj;
jasmine.createSpyObj = function() {
const args: any = Array.prototype.slice.call(arguments);
const propertyNames = args.length >= 3 ? args[2] : null;
let spyObj: any;
if (propertyNames) {
const defineProperty = Object.defineProperty;
Object.defineProperty = function(obj: any, p: string, attributes: any) {
return defineProperty.call(
this, obj, p, {...attributes, configurable: true, enumerable: true});
};
try {
spyObj = originalCreateSpyObj.apply(this, args);
} finally {
Object.defineProperty = defineProperty;
}
} else {
spyObj = originalCreateSpyObj.apply(this, args);
}
return spyObj;
};
}

/**
* Gets a function wrapping the body of a Jasmine `describe` block to execute in a
* synchronous-only zone.
Expand Down
10 changes: 10 additions & 0 deletions packages/zone.js/test/jasmine-patch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,14 @@ ifEnvSupports(supportJasmineSpec, () => {
expect(log).toEqual(['resolved']);
});
});

describe('jasmine.createSpyObj', () => {
it('createSpyObj with properties should be able to be retrieved from the spy', () => {
const spy = jasmine.createSpyObj('obj', ['someFunction'], {prop1: 'foo'});
expect(spy.prop1).toEqual('foo');
const desc: any = Object.getOwnPropertyDescriptor(spy, 'prop1');
expect(desc.enumerable).toBe(true);
expect(desc.configurable).toBe(true);
});
});
})();