What is the difference between fakeAsync and async in Angular testing?

tl;dr In almost all cases, they can be used interchangeably, but using fakeAsync()/tick() combo is preferred unless you need to make an XHR call, in which case you MUST use async()/whenStable() combo, as fakeAsync() does not support XHR calls. For the most part they can be used interchangeably. I can’t think of anything off the … Read more

Angular 2 Final Release Router Unit Test

For testing we now create a testing module using TestBed. We can use the TestBed#configureTestingModule and pass a metadata object to it the same way we would pass to @NgModule beforeEach(() => { TestBed.configureTestingModule({ imports: [ /* modules to import */ ], providers: [ /* add providers */ ], declarations: [ /* components, directives, and … Read more

How to unit test a component that depends on parameters from ActivatedRoute?

The simplest way to do this is to just use the useValue attribute and provide an Observable of the value you want to mock. RxJS < 6 import { Observable } from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; … { provide: ActivatedRoute, useValue: { params: Observable.of({id: 123}) } } RxJS >= 6 import { of } from ‘rxjs’; … Read more

Angular 2 Testing – Async function call – when to use

async will not allow the next test to start until the async finishes all its tasks. What async does is wrap the callback in a Zone, where all asynchronous tasks (e.g. setTimeout) are tracked. Once all the asynchronous tasks are complete, then the async completes. If you have ever worked with Jasmine outside out Angular, … Read more

Testing error case with observables in services

You can simply mock Observable throw error object like Observable.throw({status: 404})and test error block of observable. const xService = fixture.debugElement.injector.get(SomeService); const mockCall = spyOn(xService, ‘method’).and.returnValue(Observable.throw({status: 404})); Update 2019 : Since some people are lazy to read comment let me put this here : It’s a best practice to use errors for Rxjs import { throwError … Read more

Testing – Can’t resolve all parameters for (ClassName)

Using Jest? In case anyone gets here AND you’re using Jest to test your Angular app (hopefully we’re a growing minority), you will run into this error if you are not emitting decorators (“emitDecoratorMetadata”:true). You’ll need to update your tsconfig.spec.json file so it looks like: { “extends”: “../../tsconfig.json”, “compilerOptions”: { “emitDecoratorMetadata”: true, “outDir”: “../../out-tsc/spec”, “types”: … Read more

Angular 2 unit testing components with routerLink

You need to configure all the routing. For testing, rather than using the RouterModule, you can use the RouterTestingModule from @angular/router/testing, where you can set up some mock routes. You will also need to import the CommonModule from @angular/common for your *ngFor. Below is a complete passing test import { Component } from ‘@angular/core’; import … Read more