Mocking Every Functions of an Object with Jest and TypeScript<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Mocking Every Functions of an Object with Jest and TypeScript

Posted on: October 18, 2018

Most of the time, it is enough to simply mock a handful amount of member of an object while performing unit tests. However, if you are using the dependency injection pattern, it is faster to not send an actual object and manually cherry-pick which function to test. Since you are injected an external object, you definitely do not want to test that object and you do not want to manually create a stub object which can be time consuming.

1class MyClass{
2 constructor(public myOtherInjectedClass: IMyInjectedClassTypeHere){ ...}

The idea is to use TypeScript mapped type to create a mirror of the type you inject but instead of having the raw function as a type to have the Jest's mocking type. Changing the type will allow having a strongly typed object that has the same members but the function to be a mock. It gives the ability to use safely the Jest's mocking features. 

The first step is to create a custom type mapping. The type has a generic type that is the object you are injecting. In the example, the IMyInjectedClasTypeHere is the one containing functions and variables. We want to change all the function to become a Jest's mock type.

1type Mockify<T> = { [P in keyof T]: T[P] extends Function ? jest.Mock<{}> : T[P] };

The "Mockify" type is looping all the members and when found one that is a function return a new Mock object. Otherwise, it returns the member in its initial format.

The next step is to create a function that transform an object into the Mockify one. So far, we only have a type translation, now we need to have the logic to transform.

1function mapToMockify<T extends Object>(obj: T): Mockify<T> {
2 let newObject: Mockify<T> = {} as Mockify<T>;
3 const properties = Object.getOwnPropertyNames(Object.getPrototypeOf(obj));
4 for (let i = 0; i < properties.length; i++) {
5 newObject[properties[i]] = jest.fn();
6 }
7 return newObject;

From this point, you can invoke the function from your real instance and get a modified one ready to be injected and interrogated with all the Jest's mocking features.

1const toTest = new MyClass(mapToMockify(new MyInjectedClassTypeHere()));

To recap, in this article we saw that with the combination of TypeScript's mapped type and basic JavaScript we created an easy way to create a replica of a class that has all its functions transformed to Mock.