We talked about unit testing a couple of times last week. One of the main benefits of unit tests is that they run quickly and provide immediate feedback. As a result, we get the best return on investment when running those unit tests on a continuous integration (CI) server after each commit.
Two challenges come with running unit tests on a CI server:
- The default test runner for Angular projects (Karma) opens a Chrome browser to run the tests, which is challenging on most CI servers (typically Unix based with no UI support).
- The
ng test
command doesn’t stop on its own. It keeps watching the source code for updates and then reruns the tests.
Fortunately for us, there is a single command that addresses both problems:
Running the command will result in a single test run (thanks to --watch false
) and will run in a UI-less way (thanks to --browsers ChromeHeadless
). That’s all your CI server needs to do.
If you’re using an older version of Angular, this old tutorial of mine might help you configure Karma accordingly.