Unit testing directives in Angular 2

Unit testing in Angular 2:
Directives
Components
Services

Developing an application with Angular 2 is challenging to say the least. There is little official documentation available and you have to search the web for blogs and tie it all together to get your application working. Time to join in.

This post will explain a way to unit test a directive using Angular 2.

The directive is simple, it:

  • has an event listener for clicks
  • has an input binding
  • injects a service

Let’s start with the directive:

And now the unit test:

There are some sub-optimal things going on in the unit test I am not completely satisfied with:

  • There is a component created just to apply the directive to, I would prefer to compile a template that uses the directive.