Angular 2 Unit Testing Errors & Solutions

Solutions to some common Angular 2 unit test errors

Posted on February 11, 2017

Here’s how to fix a few common Angular 2 unit test errors:

Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’

Fix: Import FormsModule.

import { FormsModule } from '@angular/forms';

imports: [
     FormsModule
]

Can’t bind to ‘formGroup’ since it isn’t a known property of ‘fieldset’

Fix: Import ReactiveFormsModule.

import {ReactiveFormsModule} from '@angular/forms';

imports: [
     ReactiveFormsModule
]

No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.

Fix: Set the base href.

import {APP_BASE_HREF} from '@angular/common';

providers: [{provide: APP_BASE_HREF, useValue : '/' }]

No provider for Http!

Fix: Import HttpModule.

import {HttpModule} from '@angular/http';

imports: [
     HttpModule
]

Failed: Unexpected value ‘[object Object]’ declared by the module ‘DynamicTestModule’

Fix: Import your routing file. In this case mine was called app.routes.

import {routing} from '../../app.routes';

imports: [
     routing
],