I am writing some unit tests for my component and i am getting this cryptic error message. I found a similar question at Angular 2 unit testing - getting error Failed to load
I encountered the same issue using angular-cli 6, so to get the right error message one should use the following:
ng test --source-map=false
Maybe it will help someone :) .
I just ran into this error and the problem was my mocks. In the component.ngOnInit i used this.route.paramMap.subscribe(...) where route is an ActivatedRoute instance
In my test i provided a mock service like this :
providers: [
{ provide: ActivatedRoute, useValue: { snapshot: { params: { id: 1 } } } }
]
And in fact i missed to mock the paramMap method
Then i fix it adding a paramMap properties like this
providers: [
{ provide: ActivatedRoute, useValue: { snapshot: { params: { id: 1 } }, paramMap: Observable.of({get: () => 1}) } }
]
Then i don't have anymore this stupid error.
So for you, i expect the class SessionServiceStub to be incomplete or erroneous. Does it get a login method that return an Observable ?
If it's not the problem you can check the NotificationServiceStub
You should use a debugger (with Webstorm it's easy to debug step-by-step) to help you.
Been chasing this for hours. Finally discovered that the problem was simply that I had imported HttpClientModule,
but not HttpClient
:
import { HttpClient, HttpClientModule } from '@angular/common/http';
All those CORS errors, and '[Script Loader]', DOMException{stack: 'Error: Failed to execute 'send' on 'XMLHttpRequest'
stuff, and it came down to just not having HttpClient!
This is a problem with the Angular Cli
version 1.2.2 or newer. Run your test with --sourcemaps=false
and you will get the right error messages.
In Angular 4-5
ng test --sourcemaps=false
orng test -sm=false
In Angular 6+
ng test --source-map=false
See details here: https://github.com/angular/angular-cli/issues/7296