Jasmine1

Introduction

Jasmine is a behavior-driven development framework, used for testing JavaScript code. To know about behavior-driven development, we must know about test-driven development. Test-driven development, as per definition, is a software development process that relies on the repetition of a very short development cycle: first the developer writes an automated test case that defines a desired improvement or new function, then produces a minimum amount of code to pass that test, and finally refactors the new code to acceptable standards. Behavior-driven development on the other hand, is a software development process that is emerged from test-driven development. Behavior-driven development process can be called as a combination of general techniques and principles of test-driven development.

Jasmine is an independent software as it does not depend on any other software development frameworks. It does not require a Document Object Model (DOM). A basic advantage that can be called of Jasmine is that its syntax is so obvious that it’s easy to understand. It also helps you to write your tests easily.

Work with Jasmine

Function:

Let’s start with an example code that you want to test using Jasmine. We all know, in every programming language, we start with a Hello World program. Here also, let’s begin with a helloworld.js. A JavaScript function that returns “Hello World”.

Spec:

Initially, you need to grab the latest standalone version of Jasmine on your computer. They are easily available in google. All you need to do is search for it and download it. Unzip the downloaded file. The /src and /spec directories will have many files in it. You’ll have to empty them out as they are just examples which you probably won’t require. Now, the function or let’s call the helloworld.js file should be put into the /src directory. We now have created the src. What we have to do next is to create the spec.

The code has two parts. The ‘describe’ part and the ‘it’ part. The ‘describe’ part will contain the main codes or functions that does the tests. ‘Describe’ is followed by the suite which is just English language and not any code, which helps to understand what it describes. Inside of ‘describe’, you have the ‘it’ part of the code. ‘It’ part is generally called as spec. The whole code might look like classes written in a function. ‘It’ describes what the code must do in general English language and in JavaScript code. You can have any number of specs in a suite.

Matchers:

When a code is needed to be tested, you will definitely require a matcher. A matcher is something that will do the checking whether your code provides the required output. For beginners, we can use expect() and toEqual() as matchers. In our Hello World example program, we need the code to return the expected output – “Hello World”. To test this, the matchers will run as expect (helloworld()).toEqual(“Hello World”); If that comes true, the program is successfully tested with no errors reported. There are many other matchers too. Matchers are selected according to the requirement of what is to be tested and what should be returned. In addition to this, you can make your own matchers too.

Example code:

describe(“The ‘toEqual’ matcher”, function() {

it(“works for simple literals and variables”, function() {

var a = 12;

expect(a).toEqual(12);

});

it(“should work for objects”, function() {

var foo = {

a: 12,

b: 34

};

var bar = {

a: 12,

b: 34

};

expect(foo).toEqual(bar);

});

});

it(“The ‘toMatch’ matcher is for regular expressions”, function() {

var message = “foo bar baz”;

expect(message).toMatch(/bar/);

expect(message).toMatch(“bar”);

expect(message).not.toMatch(/quux/);

});

it(“The ‘toBeDefined’ matcher compares against `undefined`”, function() {

var a = {

foo: “foo”

};

expect(a.foo).toBeDefined();

expect(a.bar).not.toBeDefined();

});

it(“The `toBeUndefined` matcher compares against `undefined`”, function() {

var a = {

foo: “foo”

};

expect(a.foo).not.toBeUndefined();

expect(a.bar).toBeUndefined();

});

it(“The ‘toBeNull’ matcher compares against null”, function() {

var a = null;

var foo = “foo”;

expect(null).toBeNull();

expect(a).toBeNull();

expect(foo).not.toBeNull();

});

it(“The ‘toBeTruthy’ matcher is for boolean casting testing”, function() {

var a, foo = “foo”;

expect(foo).toBeTruthy();

expect(a).not.toBeTruthy();

});

it(“The ‘toBeFalsy’ matcher is for boolean casting testing”, function() {

var a, foo = “foo”;

expect(a).toBeFalsy();

expect(foo).not.toBeFalsy();

});

it(“The ‘toContain’ matcher is for finding an item in an Array”, function() {

var a = ["foo", "bar", "baz"];

expect(a).toContain(“bar”);

expect(a).not.toContain(“quux”);

});

it(“The ‘toBeLessThan’ matcher is for mathematical comparisons”, function() {

var pi = 3.1415926,

e = 2.78;

expect(e).toBeLessThan(pi);

expect(pi).not.toBeLessThan(e);

});

it(“The ‘toBeGreaterThan’ matcher is for mathematical comparisons”, function() {

var pi = 3.1415926,

e = 2.78;

expect(pi).toBeGreaterThan(e);

expect(e).not.toBeGreaterThan(pi);

});

it(“The ‘toBeCloseTo’ matcher is for precision math comparison”, function() {

var pi = 3.1415926,

e = 2.78;

expect(pi).not.toBeCloseTo(e, 2);

expect(pi).toBeCloseTo(e, 0);

});

it(“The ‘toThrow’ matcher is for testing if a function throws an exception”, function() {

var foo = function() {

return1 + 2;

};

var bar = function() {

return a + 1;

};

expect(foo).not.toThrow();

expect(bar).toThrow();

});

it(“The ‘toThrowError’ matcher is for testing a specific thrown exception”, function() {

var foo = function() {

thrownew TypeError(“foo bar baz”);

};

expect(foo).toThrowError(“foo bar baz”);

expect(foo).toThrowError(/bar/);

expect(foo).toThrowError(TypeError);

expect(foo).toThrowError(TypeError, “foo bar baz”);

});

});

(Code taken from http://jasmine.github.io/2.0/introduction.html)

Some advantages of using Jasmine:

  • Jasmine is independent. It does not depend on any other JavaScript frameworks.
  • It does not require a DOM.
  • It has a clean, obvious syntax.
  • Help maintainers understand the intention behind the code.
  • Brings validation and proper data handling concerns to forefront.

Conclusion:

There is a plenty more you can do with Jasmine. Overall, Jasmine makes your testing fun. So if you’re not yet into testing, now is an excellent time to start your JavaScript testing. It makes your testing pretty simple with Jasmine’s fast and simple syntax.

Shekhar R