Codementor Events

How to run Angular unit tests in CI?

Published Oct 24, 2019
How to run Angular unit tests in CI?

Whether you are an Angular beginner, or a seasoned developer - writing tests should be something you always keep in your belt, as a measure to fight off bugs and future regressions. The importance of testing is always underestimated until bugs inside the code start to pile up to an unmanagable amount.

Now that we assume that you've written some tests to cover the behavior of your Angular components, how do you run the test suite? The very basic command that the Angular CLI provides is:

ng test

However, that command is only useful during development, mainly because it opens a full blown browser (Chrome) and starts running tests there. In a CI or terminal environment, you don't have the luxury of having a desktop and a full Chrome installation. That's where the Headless Chrome comes into use. You can use it, by calling a variation of the above command:

ng test --sourceMap=false --browsers=ChromeHeadless --watch=false

What it does is:

  1. Launch Google Chrome in "headless" mode (in memory)
  2. Prevent source maps from being generated, leading to less ambiguous errors.
  3. Disable the watcher so the command exits with a status code on completion.

All three things make it the perfect command for a CI (continuous integration) environment.

Happy coding!

Discover and read more posts from Dzhuneyt A
get started
post commentsBe the first to share your opinion
Show more replies