Introduction to Test Driven Development with Karma and Jasmine


Lets begin with Jasmine library. It’s a behavior-driven development framework that gives us a set of methods to write and then execute unit tests in JavaScript. If you’re interested, can find more detail about Jasmine via the documentation here. I’ve used version 2.8.0 for my application. If you click here, you get to download the necessary files to use the library. I will use the Jasmine standalone zip folder which you can download from the link. But before, I am going to create a folder for test sample called TDD. Next step is to unzip Jasmine-standalone zip file into TDD folder. Looking into the contents of our new folder in the text editor I can see a lib folder that contains the Jasmine source files. There is also a spec and source folder that contain example logic and unit tests to help us get started. I won’t use these files since I will create our own example test. So I will go ahead and delete them but I will keep the folders since I’m going to use the same folder structure to separate our logic and our tests.

To review the Jasmine basics we will create a simple unit test for some math logic. To start off we will create a new source file called math.js and the unit test in the spec folder called math spec.js. If we open the Spec Runner HTML file in the browser we will see nice looking Jasmine interface. Now let’s have a unit test, right! I’ll open math spec file and add a describe block. The describe function is a Jasmine construct and allows us to create logically grouped unit tests by name. I’m naming it “math”, easy. Now I’m ready to add the unit test within our math block, and we can do this using the “it” construct which is also part of Jasmine package. As I’d with the describe block we need to give our “it” function a name. For the sake of this unit test I’m going to write some basic logic so to just add two numbers. So I’m gonna go ahead and name “it” accordingly, “should sum two digits”.

Next I set up the assertion for the test, by creating an expectation. The expect function is part of Jasmine and we can pass expression arguments such as primitive types, functions of objects that will be evaluated to produce a value. I can then make use of Jasmine’s matches to state what the value produced by the expectation should be. Now it’s time to use karma to execute our unit tests via the command line and that’s all I care about. Karma is installed by the NPM Package Manager which comes with Node.js. Therefore Node.js is a required install.

Now key in following commands:

$ npm install karma --save-dev

 Then go back to your package.Json file and edit scripts to:

"scripts": {
    "test": "karma start karma.conf.js"
  }

And then run following command:

$ npm test

And then you should be able to see successful unit tests being passed in your command line.

you can download the complete math-app here.

Ramin Ahmadi
I am a full-stack front end developer with over 5 years experience in web design and development. I have worked with a wide variety of environments and languages including Angular, TypeScript, NodeJS, Restful API, Microservices, Atomic design, JQuery, Material design, Progressive Web Apps, DevOps, and many Azure tools. I make it a goal to automate myself out of routine tasks in my daily work. My motto is, ‘write human readable code, lean and clean’.

Comments 0

Your email address will not be published. Required fields are marked *

Introduction to Test Driven Development with Karma and Jasmine

log in

Captcha!

reset password

Back to
log in
Choose A Format
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Poll
Voting to make decisions or determine opinions
List
The Classic Internet Listicles
Image
Photo or GIF
Gif
GIF format