Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. export type SpyOf<T> = { [R in keyof T]: Partial<jest.Mock<T[R]>> } & T;
  2.  
  3. /**
  4. * The idea behind the autospy is to pass it a function constructor(a class) and it will
  5. * return an object that can be used instead of the initial one in constructors etc.
  6. *
  7. * If you pass in the wantedReturnValues it will return those /use the functions in there
  8. */
  9. export function autoSpy<T extends Object>(obj: new (...args: any[]) => T, returnValues?: Partial<T>): SpyOf<T> {
  10. returnValues = returnValues || {};
  11. // tslint:disable-next-line:prefer-const
  12. let res: SpyOf<T> = {} as any;
  13. Object.keys(obj.prototype).forEach(key => {
  14. if (returnValues.hasOwnProperty(key)) {
  15. const valueToReturn = returnValues[key as any];
  16. if (typeof valueToReturn === 'function') {
  17. res[key] = jest.fn(valueToReturn);
  18. } else {
  19. res[key] = jest.fn(() => valueToReturn);
  20. }
  21. } else {
  22. res[key] = jest.fn();
  23. }
  24. });
  25.  
  26. return res;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement