What Is Karma?
Karma is a test runner, and not a testing framework. It is not an alternative (or in competition with) frameworks such as Mocha or Jasmine, instead it works with them. It was created by the Angular team, and there are available plugins for all the popular test frameworks. In addition, a tester can also choose to write an adapter for one of their favorite frameworks. In essence it spins up a webserver from which to run tests.
Why Karma?
The Unique Selling Point (USP) of Karma is its ability to run tests against real browsers and devices and therefore the testing produces superior, dependable and reliable results. Each browser behavior is different, and testing on all the major ones ensures the smooth functioning of the application being tested. Karma also allows testing on phones, tablets, laptops, desktops and so on, helping the developers to produce the best application that functions across a variety of on devices.
An additional unique factor about Karma is that it continuously runs tests every time there is a file change, allowing the developer to test without leaving their terminal.
Karma has over 11k stars on Github at the time of writing. It has been around since 2015 and is well maintained.
The Working Model of Karma
Being a test runner, Karma starts a web server, and serves the JS source and test files on that server. It loads all the source and test files in the correct order. Finally, it runs the tests on the browsers. Whilst this sounds easy and straightforward, Karma should not be underestimated, and it can be configured to do much more than this.
What Is Needed to Start Working With Karma?
To work with Karma, it is necessary to have an understanding of NodeJS and Node package manager. A folder is created for the application for which the tests are going to be written; this folder can have any chosen name. Next, the recommended text editor is downloaded and installed; once the ‘tester/developer’ has downloaded the text editor and the test runner, they are good to go.
Simplicity at Its Best
Most of the time, testing seems to be a painstaking task. However, Karma simplifies this and builds a productive testing environment for developers. Karma is a simple tool with no overwhelming configurations. This helps the developers to write the code and get instant feedback from the tests. Receptive feedback makes an application more productive and user friendly.
A Short Summary
Karma is not a testing framework or an assertion library. The developer/tester/coder will not be writing tests using Karma, but will be using the testing framework that they are comfortable working with. Karma works with different testing frameworks e.g. Jasmine, Mocha and so on. It ensures that every time the file changes, the test will automatically run, thus avoiding the ‘to and fro’ between files and windows. Karma also aids in testing the application on a variety of browsers and devices to bring a perfect and diverse user experience.
What Is Mocha?
“A five-minute coffee break is all that I need, while the testing tool does its job on its own.” Mocha is a flexible, adaptable and function-rich open-source test framework, used for JavaScript test applications, running on Node.js. It comes with a wide range of features that help to test synchronous and asynchronous JavaScript concepts in the browser. Before deploying into production, JS code can be extensively tested in depth using Mocha.
Mocha is extremely popular and very well maintained. It has over 21k stars on Github at the time of writing and has been around for over a decade. Its documentation is well written and easy to follow.
What Is Playwright?
Playwright is a headless browser automation platform, similar in part to Puppeteer. It can target several browsers including Chrome, Safari, Firefox and Edge. Developed and maintained by Microsoft, Playwright is a test automation framework that allows the user to perform comprehensive automated testing directly on major browsers. This tool is designed with multiple features such as an auto-wait option, capturing of test trace, flexibility and perhaps most importantly cross platform and support for several coding languages. Playwright is one of the few test frameworks that can use other languages as well as JavaScript / Typescript. Test cases can be written in .Net, Java, TypeScript and Python, and it runs cross platform on Windows, Mac and Linux.
What is Protractor?
Most importantly Protractor has been abandoned by its maintainers and is end of life Summer 2023. Given its 8.8k stars on Github and its heavy usage by certain parts of the Angular community, it is still worth understanding. Protractor is an opensource end to end testing automation tool built by Google, for Angular and AngularJS applications. Although it might be expected that the tool can only be used for Angular applications, it can also be used for non-Angular applications. The testing is automated on an actual browser.