How to stub a Typescript-Interface / Type-definition?

I work with Typescript on an AngularJS 1.X project. I use different Javascript libraries for different purposes. To unit-test my source I would like to stub some dependencies using the Typings (= interfaces). I don’t want to use the ANY-type and neither to write an empty method for each interface method.

Im looking for a way to do something like that:

let dependency = stub(IDependency);
stub(dependency.b(), () => {console.log("Hello World")});
dependency.a(); // --> Compile, do nothing, no exception
dependency.b(); // --> Compile, print "Hello World", no exception

The pain I have right now, is that I either use any and implement all methods which get called in my test case or I implement the interface and implement the full interface. That’s too much useless code :(.

How can I generate an object that has an empty implementation for each method and is typed? I use Sinon for mocking purposes, but im open to use other libraries too.

PS: I know that Typescript erases the interfaces…but I still would like to solve that :).


I think the short answer is that this is not possible in Typescript, as the language offers no compile-time or run-time “reflection”. It’s not possible for a mock library to iterate the members of an interface.

See thread:

This is unfortunate for TDD developers, in which mocking a dependency is a central part of the development workflow.

There are a number of techniques for quickly stubbing the methods, however, as described by the other answers. These options might do the job, with a little mental adjustment.

Edit: The Typescript Abstract Syntax Tree, AST, is a compile-time “introspection” – which could probably be used to generate mocks. However, I don’t know if anyone has made a practical library.

Leave a Reply

Your email address will not be published. Required fields are marked *