At 51zero, we love Angular for creating web-apps for our clients. With Angular 2.0 now firmly in Beta and stabilised, its the perfect time to show you how we're testing in Angular 2.0.

The obvious tool of choice here is Karma with Jasmine (enforced by the angular2/testing module, and also angular2 internal tests).

The key concept which we apply in all tests is to stub / spy on all external dependencies and the methods called within the unit itself. A small exception would be static methods from generic libraries e.g. Array.forEach, underscorejs etc, which do not hold state.

All of the private methods (even they are exposed as public when transpiled to js, and not truly private) should be tested strictly via the public methods/ events only. In such cases they could be either stubbed e.g. to simplify the result or mock the behaviour of them.

Angular provides a great way to do so easily with the dependency injection at the core of its framework.

Angular2 has got three types of concepts, which require slightly different setup of tests and logic of testing - Components, Pipes and Services.

The easiest of all is Pipes, as they behave as/are essentially static methods.

First of all lets define the important bits in karma.conf.js file: