Using Jasmine framework to test Angular Router

Burak Tasci
Feb 16, 2017 · 2 min read

Have an Angular application that uses the @angular/router to provide navigation across pages?

The following test sample could be scaled to your application to ensure routing is done correctly, from one component to another.

First of all, let’s import the dependencies we will need in this test:

Then, let’s create a TestBootstrapComponent which contains the router-outlet — used to bootstrap the test app; and a TestComponent.

You won’t need to create different components for each route. A dummy test component is more than enough.

Now, you will need to import these components to the app.

Keeping in mind that components can only be imported by one module, you should create a shared module and import components there — which could be imported by any feature modules in case of need:

And then, you’ll need to provide routes:

Finally, it’s time to prepare the module configuration for testing purposes:

After all, testing the route navigation across pages is as easy as below:

Burak Tasci (fulls1z3)
https://www.linkedin.com/in/buraktasci
http://stackoverflow.com/users/7047325/burak-tasci
https://github.com/fulls1z3

Burak Tasci

In depth articles about software technologies

Burak Tasci

Written by

Full-stack software engineer and enthusiastic power-lifter

Burak Tasci

In depth articles about software technologies