Setting up Karma with Jasmine
Credit : Karma - https://karma-runner.github.io/4.0/index.html

Setting up Karma with Jasmine

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. 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

No alt text provided for this image

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)

No alt text provided for this image

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



Article created : 5 May 2020

Joseph Irving D'aranjo

| Commercial and Business Development | Sustainability Advocate | Interior Designing | Hospitality | Project Management |

4 年

Morning Ivan I definitely learn something new today my friend. Thanks Have a good day :)

回复
Ivan T.

CMSAC RegCLR /CTRTC /Mental Health Counsellor / Mindfulness Coach / Automation QA / BA

4 年

要查看或添加评论,请登录

Ivan T.的更多文章

  • Short hiatus (1 month) from social media and its impact

    Short hiatus (1 month) from social media and its impact

    I am a huge fan of LinkedIn, Instagram, TikTok, YouTube, Reddit, a few e-commerce sites and other popular platforms. I…

    15 条评论
  • Solution Focused - Not Knowing Position

    Solution Focused - Not Knowing Position

    Solution Focused Brief Therapy (SFBT) is a form of therapy that has gained popularity in recent decades. It is a brief…

    9 条评论
  • Text Counselling

    Text Counselling

    Why is counselling important for our mental health Counselling is important for mental wellness because it provides a…

    13 条评论
  • Loneliness

    Loneliness

    We feel lonely from time to time. The feelings of loneliness are personal, and everyone's experience will differ.

    9 条评论
  • Struggling over popular festive season (Lunar New Year)

    Struggling over popular festive season (Lunar New Year)

    Lunar New Year is this week. It is meant to be a time of joy with celebrations, red envelopes, gifts and qualify family…

    9 条评论
  • Passing data in React between Parent and Child in Functional Components

    Passing data in React between Parent and Child in Functional Components

    For beginners who started out in ReactJS, passing data between components may be confusing. I struggle this when I…

    6 条评论
  • Getting your React project ready for?Heroku

    Getting your React project ready for?Heroku

    If you are new to React JS and want to deploy to Heroku, these are few steps to ensure that you can deploy…

    3 条评论
  • Console Tricks

    Console Tricks

    Console is is a favourite feature for many web developers. If you have been using it, you know there are lots of tricks.

    12 条评论
  • Why And How We Should Learn To Say?No

    Why And How We Should Learn To Say?No

    Are you a people pleaser ? Do you find it difficult to say "No" or reject an invitation, task or someone's request ?…

    9 条评论
  • HTTP Status Code

    HTTP Status Code

    When we visit a (web) site, it usually send some request to the server. There will be a returned code to indicate the…

    24 条评论

社区洞察

其他会员也浏览了