Getting Started with Jasmine Test Framework

Jasmine is a Javascript Testing framework which starts with behavioral specifications as a basis for writing test cases. To put it another way, you can consider user stories as a reference for write test cases. Each user story in your project can have one or more Jasmine test cases.

Majorly, Jasmine is made up of three functions

  1. describe
  2. it
  3. assertions (expect)

Like standard frameworks, it also has setup method and tear down methods

  • beforeEach() – Setup Method
  • afterEach() – Teardown Method

For the sake of simplicity of post, I would not discuss much in detail. However, you can navigate to official Jasmine guide for deep dive.

Download Jasmine

You can download latest version of Jasmine from here. Once downloaded, you can extract and save lib folder in your project.

Simple Javascript code for Jasmine Test Framework

Let’s consider below Javascript code. It simply declares a person object with few properties and method to get full name and change Age.

Its time to see how we can write a test method using Jasmine Framework.

Above code snippet shows how we are using describe, it and expect methods provided by Jasmine framework.

Let’s create a sample HTML page which will load Jasmine Framework, our Javascript code and Test method to Test our code.

Below is our files and folders structure

Jasmine Test Framework folder structure
Jasmine Test Framework folder structure

Below is the output of our example. As all test passed, we don’t see any error

Jasmine Test Framework Success Output
Jasmine Test Framework Success Output

In case, if some method fails test execution, then it would show error report like below image

Jasmine Test Framework Error Report
Jasmine Test Framework Error Report

First 25 minutes of below video is basics of Jasmine Test Framework with same example as of this post.

 

Posted

in

by


Related Posts

Comments

One response to “Getting Started with Jasmine Test Framework”

  1. […] Getting Started with Jasmine Test Framework […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading