Merge branch 'enelson/spyonall' of https://github.com/elliot-nelson/jasmine into elliot-nelson-enelson/spyonall

- Merges #1699 from @elliot-nelson
- Fixes #1677
This commit is contained in:
Gregg Van Hove
2019-05-14 18:00:20 -07:00
3 changed files with 73 additions and 13 deletions

View File

@@ -6471,13 +6471,25 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
throw new Error('spyOnAllFunctions could not find an object to spy upon'); throw new Error('spyOnAllFunctions could not find an object to spy upon');
} }
for (var prop in obj) { var pointer = obj,
if (Object.prototype.hasOwnProperty.call(obj, prop) && obj[prop] instanceof Function) { props = [],
var descriptor = Object.getOwnPropertyDescriptor(obj, prop); prop,
if ((descriptor.writable || descriptor.set) && descriptor.configurable) { descriptor;
this.spyOn(obj, prop);
while (pointer) {
for (prop in pointer) {
if (Object.prototype.hasOwnProperty.call(pointer, prop) && pointer[prop] instanceof Function) {
descriptor = Object.getOwnPropertyDescriptor(pointer, prop);
if ((descriptor.writable || descriptor.set) && descriptor.configurable) {
props.push(prop);
}
} }
} }
pointer = Object.getPrototypeOf(pointer);
}
for (var i = 0; i < props.length; i++) {
this.spyOn(obj, props[i]);
} }
return obj; return obj;

View File

@@ -222,7 +222,7 @@ describe("SpyRegistry", function() {
}).toThrowError(/spyOnAllFunctions could not find an object to spy upon/); }).toThrowError(/spyOnAllFunctions could not find an object to spy upon/);
}); });
it("overrides all writable and configurable functions of the object", function() { it("overrides all writable and configurable functions of the object and its parents", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: function() { var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: function() {
return 'I am a spy'; return 'I am a spy';
}}); }});
@@ -234,7 +234,7 @@ describe("SpyRegistry", function() {
var noop5 = createNoop(); var noop5 = createNoop();
var parent = { var parent = {
notSpied1: noop1 parentSpied1: noop1
}; };
var subject = Object.create(parent); var subject = Object.create(parent);
Object.defineProperty(subject, 'spied1', { Object.defineProperty(subject, 'spied1', {
@@ -291,7 +291,7 @@ describe("SpyRegistry", function() {
var spiedObject = spyRegistry.spyOnAllFunctions(subject); var spiedObject = spyRegistry.spyOnAllFunctions(subject);
expect(subject.notSpied1).toBe(noop1); expect(subject.parentSpied1).toBe('I am a spy');
expect(subject.notSpied2).toBe(noop2); expect(subject.notSpied2).toBe(noop2);
expect(subject.notSpied3).toBe(noop3); expect(subject.notSpied3).toBe(noop3);
expect(subject.notSpied4).toBe(noop4); expect(subject.notSpied4).toBe(noop4);
@@ -303,6 +303,42 @@ describe("SpyRegistry", function() {
expect(subject.spied4).toBe('I am a spy'); expect(subject.spied4).toBe('I am a spy');
expect(spiedObject).toBe(subject); expect(spiedObject).toBe(subject);
}); });
it("overrides prototype methods on the object", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: function() {
return 'I am a spy';
}});
var noop1 = function() { };
var noop2 = function() { };
var MyClass = function() {
this.spied1 = noop1;
};
MyClass.prototype.spied2 = noop2;
var subject = new MyClass();
spyRegistry.spyOnAllFunctions(subject);
expect(subject.spied1).toBe('I am a spy');
expect(subject.spied2).toBe('I am a spy');
expect(MyClass.prototype.spied2).toBe(noop2);
});
it("does not override non-enumerable properties (like Object.prototype methods)", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: function() {
return 'I am a spy';
}});
var subject = {
spied1: function() { }
};
spyRegistry.spyOnAllFunctions(subject);
expect(subject.spied1).toBe('I am a spy');
expect(subject.toString).not.toBe('I am a spy');
expect(subject.hasOwnProperty).not.toBe('I am a spy');
});
}); });
describe("#clearSpies", function() { describe("#clearSpies", function() {

View File

@@ -125,13 +125,25 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
throw new Error('spyOnAllFunctions could not find an object to spy upon'); throw new Error('spyOnAllFunctions could not find an object to spy upon');
} }
for (var prop in obj) { var pointer = obj,
if (Object.prototype.hasOwnProperty.call(obj, prop) && obj[prop] instanceof Function) { props = [],
var descriptor = Object.getOwnPropertyDescriptor(obj, prop); prop,
if ((descriptor.writable || descriptor.set) && descriptor.configurable) { descriptor;
this.spyOn(obj, prop);
while (pointer) {
for (prop in pointer) {
if (Object.prototype.hasOwnProperty.call(pointer, prop) && pointer[prop] instanceof Function) {
descriptor = Object.getOwnPropertyDescriptor(pointer, prop);
if ((descriptor.writable || descriptor.set) && descriptor.configurable) {
props.push(prop);
}
} }
} }
pointer = Object.getPrototypeOf(pointer);
}
for (var i = 0; i < props.length; i++) {
this.spyOn(obj, props[i]);
} }
return obj; return obj;