How To Level Up Your Angular Unit Testing Game (1/3)
Tags: testing, angular, tips, webdev
Published: 2019-01-14 21:53:04

---
title: How To Level Up Your Angular Unit Testing Game (1/3)
published: true
description: Tips to run Angular unit tests and customize your test tooling
tags: testing,angular,tips,webdev
cover_image: https://github.com/alisaduncan/tutorial-angular-httpclient/blob/test-configuration/post-images/level-up-mario.jpg?raw=true
series: level-up-angular-unit-testing 
---

Unit tests sure are useful. Having a good set of tests around your application helps catch errors more quickly and makes refactoring code without regression easier. Does it mean your application won't have bugs? Nope. But I love having the confidence that I'm on the right track and not making silly mistakes when I refactor or add a new feature. And I'm so glad Angular (and Angular CLI) had testing in mind from day one.

There's so much we can do with unit testing in Angular. **So** much that this is the first article in a series of 3.

In this post, we'll learn some techniques to level up your testing game using what Angular CLI provides out of the box and get pumped up to unit test!

![](https://media.giphy.com/media/bdpIDWZFkI67S/giphy.gif)


# Follow Along

We're using Angular CLI to test the application and using code from a previous post I wrote. 

{% link https://dev.to/alisaduncan/intercepting-http-requests---using-and-testing-angulars-httpclient %}

You can clone the [tutorial-angular-httpclient](https://github.com/alisaduncan/tutorial-angular-httpclient) and run tests too. All the level ups discussed in the posts are found in "test-configuration" branch.


### Assumptions

> I'm also making the assumption you know [why unit tests are helpful](https://dzone.com/articles/top-8-benefits-of-unit-testing), [how to write unit tests](https://angular.io/guide/testing), and [when to write unit tests](https://medium.freecodecamp.org/why-end-to-end-testing-is-important-for-your-team-cb7eb0ec1504) (versus end-to-end tests).


# Angular CLI 

When you create an Angular app using the CLI, it scaffolds everything you need for testing. Angular's CLI automatically stubs out tests for you when you use it to generate code. And it makes running your test a breeze. Under the covers it uses Karma and Jasmine for unit tests. If Jasmine and Karma is not your favorite, you can change it. But we'll use the default tools in this series of post. 

Whatever testing frameworks you like, use it. Just don't be this guy.

![](http://www.quickmeme.com/img/70/70710c8aff156b84becbd522bea259023fb64be7369ec8c3bc9638f348284ab1.jpg)


## Running Tests

Ready to test? Let's go!

![](https://angular.io/generated/images/marketing/concept-icons/cli.png)

Using the command `ng test` automatically starts running your test in Chrome in watch mode. That means you can make changes in your code and it will automatically re-run tests for you.

![](https://github.com/alisaduncan/tutorial-angular-httpclient/blob/test-configuration/post-images/karma.png?raw=true)

Awesome! That's super helpful while writing feature code!

Now you can happily code away to knock that feature out.... Oops! The feature code caused an existing unit test to fail! How can we figure out why a test is failing? Can we debug unit tests? Why yes, we can.

![](https://media.giphy.com/media/KrV78CrSWtPDa/giphy.gif)


# Debugging Tests In Chrome

Depending on whether you are using Angular v6 or Angular v7, the instructions are different. In Angular v7, after starting `ng test`, you can debug an individual test by following the steps below:
1. Press **DEBUG** in Karma runner
1. Open Chrome DevTools and navigate to **Sources**
1. Expand src and find the test you want to step in to and set breakpoints
1. Press the link on the test you want to run in the Karma runner in Chrome

![](https://github.com/alisaduncan/tutorial-angular-httpclient/blob/test-configuration/post-images/karma-debug.png?raw=true)

In Angular v6, after starting `ng test`, you can debug an individual test by following the steps below:
1. Press **DEBUG** in Karma runner
1. Open Chrome DevTools and navigate to **Sources**
1. Expand webpack:// &rarr; \<repo file path\> &rarr; src &rarr; app
1. Find the test you want to step in to and set breakpoints
1. Press the link on the test you want to run in the Karma runner in Chrome

You can set breakpoints and step into in the code the unit tests calls, not just in the unit test itself. So do you want to step through your Angular service code via a the unit test? No problem! Treat unit test debugging just like you would debugging your application normally.

![](https://media.giphy.com/media/14nakW0jC4HA6k/giphy.gif)


Running tests in watch mode is great when actively writing code, but sometimes you want to run the test just once. Or you want to run the tests in headless mode.  Angular CLI has just the mechanism to support this using a variety of [flags you can use](https://github.com/angular/angular-cli/wiki/test). 


# Pass In Flags

Want to run tests only once instead of continuously? This is useful if you want to run tests one last time when you're finished with your story or as part of the CI process. Since it's time to check in your new code, kick off running your tests with the `watch` flag set to false.


```bash
ng test --watch false
```

Yes! But build processes usually also needs to run headless. You can stack flags together to run your tests both headless and once.

```bash
ng test --watch false --browsers ChromeHeadless
```

![](https://github.com/alisaduncan/tutorial-angular-httpclient/blob/test-configuration/post-images/ng-test.png?raw=true)

Don't forget you can create a npm script to run this test configuration to save you seconds of typing!

![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT8OCWc4vgZdLkv13SgIKM6AdqzcVL85gEkl7JZTXZiJ_oPqyVu)


Now that we've covered the basics of using Angular CLI to run unit tests, we'll dive deeper into Karma configuration and customizing the test runner in the next post in this series.