JavaScript testing with Karma

JavaScript testing with KarmaUnit tests are a major enhancer of productivity, especially if approached and written properly. JavaScript as one of the most popular programming languages for the web needs effective testing. Different tools have been developed to assist with this task. Here we will talk about one of them - Karma.

Karma is a simple and lightweight open source test runner for JavaScript. It is supported by the Angular.js team at Google and well suited for testing AngularJS code or any other JavaScript projects. Node.js is required for running Karma, though you don't have to write your code using Node modules. With Karma it is easy to execute tests locally during development. The whole workflow can be controlled from the command line or IDE.

Karma is neither a testing framework, nor an assertion library. It just launches instances of the chosen web browsers, loads the files you specify, and displays the test results against each browser in the terminal. You can integrate Karma with any other testing tools. For instance, there is excellent plugin support for many popular JavaScript testing suites such as Jasmine, Mocha, and QUnit.

One of the biggest advantages of Karma is testing on real devices: you can execute JavaScript code in multiple real browsers and platforms of your choice, like desktop, phones, tablets or on a headless PhantomJS instance. Available browsers include Firefox, Chrome and Chrome Canary, Opera, Safari, IE, PhantomJS, SauceLabs, BrowserStack, and many more.

Karma aims at creating a productive testing environment for developers, so its great benefit is re-running tests when files change. This tool can be instructed to watch all the files, specified within the configuration file. Whenever changes are added and saved to the file, all of the captured browsers are triggered to run the test code again. Instant feedback from tests makes test writing more productive and fast.

Karma is highly configurable and easily debugged. Its tests can be executed on a continuous integration server. Karma can be easily integrated with such packages as Jenkins, Travis, and Semaphore. If you want to learn a little more about Karma, check out the Karma website.