Skip to content

Instantly share code, notes, and snippets.

@Devcon4
Last active August 7, 2018 16:41
Show Gist options
  • Save Devcon4/056d8f2db15de76989225f400f222085 to your computer and use it in GitHub Desktop.
Save Devcon4/056d8f2db15de76989225f400f222085 to your computer and use it in GitHub Desktop.
export type Spied<T> = {
[K in keyof T]: T[K] extends Function ? jasmine.Spy : T[K] extends object ? Spied<T[K]> : T[K];
};
export function spyOnClass<T, U1 extends unknown[]>(type: new (...tArg1: U1) => T, ...arg1: U1) {
let spyOnFunctions = function <W>(obj: W): Spied<W> {
Object.keys(obj).forEach(p => {
if (!!obj && !arg1.some(a => obj[p] === a)) {
if (obj[p] instanceof Function) {
spyOn(obj, p as keyof W);
}
else if (!!obj[p] && typeof obj[p] === 'object') {
obj[p] = spyOnFunctions(obj[p]);
}
}
});
return obj as Spied<W>;
};
let ctor = new type(...arg1 as U1);
return spyOnFunctions(ctor);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment