The reason we test our code is to point out the defects and errors that were introduced during the development phases. I have been part of many projects in which developers used to update a small piece of code and then manually opens their browser to verify that it still works and behaves as they expect it to. This approach (manual QA) is begging for a disaster.
If a feature or function is removed, Do every developers remember all of its potential side-effects? Do all developers manually test in the same way? Probably not. And the downside of manual testing is: “As features and codebases grow, manual QA becomes more time consuming, expensive and error prone”. These are the reasons as to why software industries are considering automation testing to be the best way to prevent software defects and adopting it over manual testing.
Karma runs on Node.js and is available as NPM package. So, to set up Karma test runner, we need Node with NPM already installed. Then, we install karma and other required plugins.
Installing Karma and plugins:
The most recommended approach is to install Karma (and all the plugins our project needs) locally in the project’s directory.
Command to install Karma:
npm install karma –save-dev
Command to install plugins that our project needs:
npm install karma-jasmine karma-chrome-launcher –save-dev
The above commands will install karma, karma-jasmine and karma-chrome-launcher packages in node_modules directory within current working directory and also save these as dev dependencies in package.json. The benefit of doing so is, any other developer working on the project will only have to do “npm install” so as to get all these dependencies installed.
Karma needs to know about the project in order to test it and this is done using a configuration file. So, we must configure Karma before we run it. This is one of the most important step in setting up Karma. The easiest way to begin is to run the init command. In a command window, we navigate to our project folder and enter “karma init” that creates a configuration file with its default name i.e. karma.conf.js in the same folder. If we want this file to be of a particular name then we enter “karma init filename”. This asks us a series of questions that are easy to answer and then generates a properly structured Karma configuration file. We will need to answer the below questions:
– What testing framework do you want to use?
Jasmine, QUnit and Mocha are installed by default and these can be referenced by name, but if we install another one we should name it here. Each framework is a plugin, so we need to list the plugin file in the plugins section of the configuration file.
– Do you want to use Require.js?
Require.js is a lazy-loading framework used by many developers to minimize the initial loading time of script in the browser. If our project uses it, we need to answer YES to this question.
– Do you want to capture a browser automatically?
We should respond with the browser name(s) that we want to use, one per line. Note that we must have the corresponding –launcher plugin installed for each browser, but only the karma–chrome-launcher and karma–chromeCanary-launcher plugin are installed by default. When we enter a blank line, it will move us to the next question.
– What is the location of your source and test files?
We need to specify the pattern that defines the location of the test files (containing tests) to be involved in testing or excluded from it as well as the files that we plan to run our tests against. Here, we can use glob patterns like “test/*.js”, “src/*.js”, “test/**/*spec.js” etc. This path is relative to the location of Karma. We can enter an absolute path and be confident of directing Karma to the right location, or enter a relative path if our files are placed below the Karma folder.
– Should any of the files included by the previous patterns be excluded?
If we use a very broad pattern, we may want to exclude the folder where there are no .js files
to test, or where we store our images. Strong patterns occupy more lines in the configuration file, but also remove the requirement for an exclude block.
– Do you want Karma to watch all the files and run tests on change?
Karma running continuously is very helpful as we can see our tests and code produced in a TDD environment. If we don’t do TDD, we can select to only run Karma when we are ready to execute our tests.
To start Karma, We fire the following command in the project root where “karma.conf.js” file resides:
In case if we have explicitly named the “karma.con.js” file other than its default name then we fire:
karma start filename
We are not covering Jasmine.