Setting up Karma with Jasmine

Ivan
3 min readMay 5, 2020

There are many tools for API testing. You can use the popular Postman, JMeter, RestAssured, Karate and many others. In this article, we are going to talk about setting up Karma — Jasmine

Why Karma — Jasmine ? In a few API tools that I have tried out, Postman was the most convenient way of minimum setup for majority of API testing. However, there are some limitation especially when I come to want to customize the flow of testing. The few others such as RestAssured and Karate seem to work fine for majority of the scripts. Till I try the Jasmine — Karma , I am wooed. Yes , it is not a new tool , Jasmine has been around for quite a while (since 2010) but it is consider one of the most flexible API tools that developers used.

Karma is a tool which lets us spawn browsers and run Jasmine tests inside of them all from the command line. The results of the tests are also displayed on the command line. Karma can also watch your development files for changes and re-run the tests automatically.

Karma/Jasmine works very well for asynchronous and in many way , I prefer the way of handling with promises and async in Jasmine than other tools as it offer the flexibility like development with minimum constraints.

If you have tried on mocha / chai , it is similar in many ways. Karma / Jasmine takes slightly more setup.

So in actual fact, you can do your scripts as BBD in Jasmine , setup a CI/CD and execute from a test runner like Karma, which can run multiple of your selected test script. It is convenient and it will re-run the test automatically. It is also very light weight compare to most API tools, and extremely fast during execution. How cool is that !

Setting up

Make sure you have node and npm install. If you have not, download it from here

Setting up npm package in your root folder. In Terminal / CMD

npm init -y

Install Jasmine

Terminal / CMD

npm install jasmine --save-dev

Install Karma

Terminal / CMD

npm install jasmine-core karma karma-chrome-launcher karma-jasmine karma-jasmine-html-reporter karma-spec-reporter --save-dev

Install browserfly

Terminal / CMD

npm install --save-dev karma-browserify browserify watchify

If you like me have external modules that need require, the browser in karma does not support it. You will need to have browserfly. This can read your external module that have require , etc const fs= require (‘fs’)

karma.conf.js

Create this file name “karma.conf.js” in your project root folder.

Add this line in the file

// Karma configuration
module.exports = function(config) {
config.set({

basePath: '',
frameworks: ['jasmine', 'browserify'],
files: [
'test/*.js'
],
exclude: [
],
preprocessors: {
'test/*.js': [ 'browserify' ]
},
plugins: [
require ('karma-browserify'),
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-spec-reporter'),
require('karma-jasmine-html-reporter')
],

reporters: ['spec','kjhtml'],
port: 9876,
colors: true,

logLevel: config.LOG_DISABLE,
autoWatch: true,
browsers: ['Chrome'],
client: {
clearContext: false
},

singleRun: false,
concurrency: Infinity,
})
}

Folder structure

This is my folder structure for the API testing. The test script is under the “test/” folder. You can change it to any name. Just make sure that you amend this in the karma.conf.js under files

files: [
'YOUR_TEST_FOLDERNAME/*.js'
],

Execute the test

In the project root folder

Terminal / CMD

karma start

And it will run the scripts under test folders!

Report

This is the report from your browser. Mine is chrome. You can use the browser as your preference (but do change in karma config)

You can add this to your CI/CD and run it.

Article created : 5 May 2020

--

--

Ivan

I am a Software Engineer and Psychotherapist. Follow me on Linkedin at linkedin.ivantay.org