Let’s first install Karma. It is available as a node package, so we can use npm to install it.
Here we are using Karma for running our unit tests. Karma launches a web server that executes the test code against connected web browsers. The tests can run manually or it can also watch files, matching certain grep pattern. After running tests, it can show the results in the command line console. You can read more about karma here.
We also need to add support to run it in a browser. Here we are installing chrome launcher for karma.
After installing the above packages, you should have a folder created node_modules containing the following packages.
npm – Node Package Manager
Here we are using npm to install these node packages. If you don’t have it installed already, you can get it directly from npmjs.com.
After the above configuration, you should have the following file created in your folder.
Writing Unit Tests
Let’s see below how easy is to write our first unit tests. We need to group the tests in describe blocks. Each test is blocked in it blocks. The assertions are added using various expect features provided by the framework.
Before running the tests, we first need to launch karma. If you have followed the above steps, it should also launch chrome and connect it to the karma.