Introduction

Jasmine is an open source behavior-driven development (BDD) framework for testing JavaScript code. Jasmine does not have dependencies on any JavaScript framework, and it does not need a Document Object Model (DOM).

A Jasmine test suite starts by calling the global describe function that takes two distinct parameters - a string and a function. The string is where you give a name for the suite, describing what you are testing. The function parameter executes the tests (called specs).


describe("some suite", function() {

});

A spec is a test which is set by calling the global Jasmine function it inside the describe block. it is an individual test specification. it also takes two parameters - a string and a function. The string parameter is where the title of the spec is set and the function parameter is the spec itself.


describe("some suite", function() {
  it("some spec", function() {
   
  });
});

Inside the spec are expectations which comprises of the expect function taking an actual value chained to another function which takes the expected value, called the matcher function.

Matchers perform a boolean comparison between the actual value and the expected value based on which Jasmine passes or fails the spec. toBe is one of the common Jasmine matcher, which is the equivalent of the === operator. Below is an example of a spec using the toBe matcher for checking whether the addition of 1+1 is correct or not


describe("Addition", function() {
  it("1+1 equals 2", function() {
   expect(1+1).toBe(2);
  });
});

A callback function can also be passed as a parameter to the expect function. Below we make use of the toEqual matcher to match the return value of the pi function against the number 3.141.


function pi() {
	return 3.141;
}

describe("Pi", function() {
  it("equals 3.141", function() {
   expect(pi()).toEqual(3.141);
  });
});

The toEqual() matcher is similar to the toBe() matcher in testing primitives (numbers, strings, booleans) but differs in object-level testing. More on the difference between toEqual() and toBe() matchers can be found here: jasmine-toequal-vs-tobe.html

More on Jasmine default matchers can be found under the following link http://jasmine.github.io/2.0/introduction.html