Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Hero detail: Integrated routed component test suite.
<div *ngIf="hero">
{{ | uppercase}} Details
<input [(ngModel)]="" placeholder="name">
<button (click)="goBack()">go back</button>
<button (click)="save()">save</button>
import { Component } from '@angular/core';
import {
} from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { of } from 'rxjs';
import { Hero } from '../hero';
import { HeroService } from '../hero.service';
import { HEROES } from '../mock-heroes';
import { HeroDetailComponent } from './hero-detail.component';
template: '<router-outlet></router-outlet>',
class TestRootComponent {}
describe('HeroDetailComponent (integrated)', () => {
function advance() {
function getTitle() {
const element = rootFixture.debugElement.query(By.css('h2'))
.nativeElement as HTMLElement;
return element.textContent.trim();
function navigateByHeroId(id: number) { => router.navigate(['detail', id]));
beforeEach(async () => {
const fakeService = {
getHero(id: number) {
const hero = [...fakeHeroes].find(h => === id);
return of(hero);
} as Partial<HeroService>;
declarations: [
imports: [
{ path: 'detail/:id', component: HeroDetailComponent },
providers: [
{ provide: HeroService, useValue: fakeService },
await TestBed.compileComponents();
rootFixture = TestBed.createComponent(TestRootComponent);
router = TestBed.inject(Router);
const fakeHeroes: ReadonlyArray<Hero> = [...HEROES];
let router: Router;
let rootFixture: ComponentFixture<TestRootComponent>;
it("displays the hero's name in upper-case letters", fakeAsync(() => {
const [expectedHero] = fakeHeroes;
import { Location } from '@angular/common';
import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Hero } from '../hero';
import { HeroService } from '../hero.service';
selector: 'app-hero-detail',
styleUrls: ['./hero-detail.component.css'],
templateUrl: './hero-detail.component.html',
export class HeroDetailComponent implements OnInit {
@Input() hero: Hero;
private route: ActivatedRoute,
private heroService: HeroService,
private location: Location,
) {}
ngOnInit(): void {
getHero(): void {
const id = +this.route.snapshot.paramMap.get('id');
.subscribe(hero => this.hero = hero);
goBack(): void {
save(): void {
.subscribe(() => this.goBack());
Copy link

meirka commented Jan 20, 2022

Hi @LayZeeDK, I have similar question:

I use jasmine test to navigation between components, for example: click link, wait for page to change and assert information on the page.
routeLink or router.navigateByUrl(..) works in production and jasmine tests. However when i'm going: this.location.back(); it only works in production, in jasmine test it doesn't navigate back.

Is there a way to use location.back() and have router change page in jasmine test?

Thank you!

Copy link

LayZeeDK commented Jan 20, 2022

Hi @meirka

If you're using Karma+Jasmine, importing the RouterTestingModule prevents Angular's Location and Router services from changing the actual URL of the browser controlled by Karma. However, the component in your test component's router outlet should change.

Copy link

meirka commented Jan 21, 2022

Can you take a look at my simple example? I created it just to make sure we are on the same page:

It looks to me that Location actually changes pages (via angular routing)... lol. I wonder what broke in my actual project, since location.back() doesn't seem to work there :(

Copy link

I identified a few issues and suggested some stylistic changes to your repo: meirka/angular-unit-test-navigation#1

Copy link

meirka commented Jan 23, 2022

Thank you, I'll go over your changes.

I'm attempting to do kinda full integration test: navigation, fill forms, backend calls (mocked by testingController) and all of it from Jasmine tests. I don't want to do e2e (for multiple reasons - long story).

Copy link

That's exactly why I built Spectacular, in particular its Feature testing API, preferably used with Angular Testing Library and/or Angular CDK Component Harnesses as described in the docs. Slide deck from NG Poland 2021 coming soon at

Copy link

meirka commented Jan 25, 2022

Thank you, I'll check it out!
I guess I'm not only one who doesn't want to waste time on e2e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment