Quick Reference to Jasmine – Javascript Unit Testing Framework

The article represents the fundamentals concepts and code samples around Jasmine, a popular Javascript unit testing framework.TO get started, download the framework from https://github.com/pivotal/jasmine/releases. For detailed examples, look at http://jasmine.github.io/edge/introduction.html.
It All Starts with “describe” and “it” Function Blocks

In Jasmine terminologies, a set of tests is collectively called as a “suite”. The test suite is defined using “describe” function block. Each test is called as a “spec” and defined using “it” function block.

A test suite can have multiple test specs and also, test suites. This implies that a “describe” function block can have multiple “it” function blocks and also, “describe” function blocks contained within.

One of the key aspect of writing tests is naming “describe” and “it” function block. I follow the rule where “describe” name consist of subject (nouns) and “it” function block name could comprise of text starting with “should” following by phrases representing different functions that could be performed. Remember that functions could be, broadly, classified into two types which are data and transaction function. Thus, the unit test specs should cover both, data and transaction functions related with a particular object put under test.

Following is syntax used to define “describe” and “it” function blocks:

describe( "text representing suite", function() {
    it("should followed by action/function", function(){
    });
});

Following are some of the examples:

//Customer.js 

function Customer(name) {
    this.name = name;
}

Customer.prototype.isAdult = function(){
    if( this.age < 18 ) return false;
    return true;
}

//CustomerSpec.js

describe( "A customer", function() {

    var customer;

    beforeEach(function(){
        customer = new Customer( "Chris" );
    });

    it( "should have a name", function(){
        expect(customer.name).toBeDefined();
        expect(customer.name).not.toBeNull();
        expect(customer.name).toEqual("Chris");
    });

    it( "should not be adult if age < 18", function(){      
             customer.age = 19;
       expect(customer.isAdult()).toBe(false);   
    });  

    it( "should be adult if age >= 18", function(){
     customer.age = 18;
     expect(customer.isAdult()).toBe(true);
     customer.age = 25;
     expect(customer.isAdult()).toBe(true);
 });
});

 

Expectations – Test Specs Building Blocks

Within each test spec (it function block), there are one or more expect statement followed by matcher function. Each matcher implements a boolean comparison between the actual value and the expected value. It is responsible for reporting to Jasmine if the expectation is true or false. Jasmine will then pass or fail the spec. Following are some samples from above example:

expect(customer.name).toBeDefined();
expect(customer.name).not.toBeNull();
expect(customer.name).toEqual(“Chris”);
expect(customer.isAdult()).toBe(true);

Following is a list of matcher functions:

  • toBe
  • toEqual
  • toBeDefined
  • toBeUndefined
  • toMatch
  • toBeTruthy
  • toContain
  • toBeGreaterThan
  • toBeCloseTo
  • toThrow
  • toThrowError
  • toBeNull
  • toHaveBeenCalled (Spies)
  • toHaveBeenCalledWith (Spies)

 

Setup & Teardown Function Blocks

Following are different methods/functions which, if defined, gets executed before and after each the test run:

beforeEach: This method makes sure that code within gets executed before each test is run. Primarily, initialization code is placed within this method.

beforeEach(function() {
    // Initialization code
  });

afterEach: This method makes sure that code within gets executed after each test is run. This is used primarily to reset the intialization parameters.

afterEach(function() {
    // Code goes here
  });

This blog is written primarily to act as a quick reference to key concepts of Jasmine framework.

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Recent Posts

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

3 hours ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

9 hours ago

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

1 day ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

1 day ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

1 day ago

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

6 days ago