Fix Angular 2 Tests with Karma

Posted by in Software

Below are some fixes I had to apply in order to make Angular 2 tests work with karma.

Binding

  • Error: Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’
  • Solution: Import FormsModulein your test:

Unknown component

  • Error: ‘custom-component’ is not a known element
  • Solution: Declare the component in your test:

Note: If the element is in another module, then import the other module (like in the previous case) instead of declaring elements one by one.

Unknown <router-outlet> tag

  • Error: ‘router-outlet’ is not a known element
  • Solution: Import RouterTestingModule:

Note: This list contains issues I’ve encountered. Please feel free to send me more.

HTH,


A little experiment: If you find this post and ad below useful, please check the ad out :-)