Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export type SpyOf<T> = { [R in keyof T]: Partial<jest.Mock<T[R]>> } & T;
- /**
- * The idea behind the autospy is to pass it a function constructor(a class) and it will
- * return an object that can be used instead of the initial one in constructors etc.
- *
- * If you pass in the wantedReturnValues it will return those /use the functions in there
- */
- export function autoSpy<T extends Object>(obj: new (...args: any[]) => T, returnValues?: Partial<T>): SpyOf<T> {
- returnValues = returnValues || {};
- // tslint:disable-next-line:prefer-const
- let res: SpyOf<T> = {} as any;
- Object.keys(obj.prototype).forEach(key => {
- if (returnValues.hasOwnProperty(key)) {
- const valueToReturn = returnValues[key as any];
- if (typeof valueToReturn === 'function') {
- res[key] = jest.fn(valueToReturn);
- } else {
- res[key] = jest.fn(() => valueToReturn);
- }
- } else {
- res[key] = jest.fn();
- }
- });
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement