Although the angular-cli is a great time saving tool for creating your angular apps, it reserved a little surprised for me the first time I used it to create an Angular application in my Debian Linux box. A surprise in the form of this message, that appeared when I tried to run the application default tests:
No binary for Chrome browser on your platform
Indeed, the default karma configuration of an angular application created with angular-cli expects to use Chrome to launch the tests.
We can verify that, if we check the karma.conf.js file to see the value of the ‘browsers’ property:
In many Linux distributions Chrome is not installed by default, and even if you installed it chances are that you did not configure the CHROME_BIN environment variable karma is using to find the chrome binaries – which prompt another little warning:
Please, set “CHROME_BIN” env variable.
So if we want to get ride the above errors and run our Angular tests we have 2 options:
- Install Chrome – or Chromium – ,and configure CHROME_BIN
- Modify the karma configuration to use a different browser.
Setting the karma launcher for chrome in Debian Linux
Let’s see how to go about getting Chrome to work. First we need to install chrome/chromium.
Then we need to configure the “CHROME_BIN” environment variable:
Now when run the tests:
A chrome window will open, and the tests will be launched.
Running Karma tests in Firefox
As I pointed above, if we do not want to launch the tests using Chrome, we can simply configure karma to launch the tests using our current browser. A quite extended browser choice for Linux distributions is Firefox. Let’s see how we would modify the Karma configuration to use Firefox.
The first thing to do is to install the karma firefox launcher:
npm install karma-firefox-launcher --save-dev
Next we need to edit the karma.conf.js file to the firefox launcher to the list of available plugins:
plugins: [ require('karma-jasmine'), require('karma-firefox-launcher'), require('karma-jasmine-html-reporter'), require('karma-coverage-istanbul-reporter'), require('@angular-devkit/build-angular/plugins/karma') ],
And lastly we modify again in thekarma.conf.js file the browsers property to specify we want to use Firefox.
And that’s all 🙂