My intention in this post it to give you a rough and basic idea of how to use the Jasmine testing framework.

I am basically getting a lot of ideas from Jasmine's website

What is Jasmine?

Princess Jasmine

Yes, she is the Aladdin's princess.... but also...

"Jasmine is a framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM"

Ok, so how can I start writing tests?

Well, easy...let's say we want to test our function pumbaa() always returns the string "Hakuna Matata". We must start by describing what we want to test. :

describe("pumbaa", function() {});

Yes, in order to describe what we want to test, we must use the 'describe' function provided by Jasmine...that contains 2 parameters:
1. The string that properly describes what is the component we want to test.
2. A function that will contain the set of tests we want to create for this particular feature.

In our above mentioned case, we want to test the right string is returned:

describe("pumbaa", function() {
    it("should return its happy phrase", function() {
      });
});

At this stage, we created:
1. A function that describes what we want to test.
2. A function inside the first one, that speficied what feature we want to test.

Ok, but what about the test itself?

Well, this is where we introduce the expect function (Made in Jasmine) that will evaluate that whatever we expect IS TRUE.

describe("pumbaa", function() {
    it("should return its happy phrase", function() {
        expect(pumba()).toBe("Hakuna Matata");
      });
});

We expect pumba() to be "Hakuna Matata"...

Of course, we can keep adding the tests we want for a given component we want to test.
To make this tests a bit more complex, let's test another function called localise() that recives a country and returns the continent:

describe("localise", function() {
    it("should localise American countries", function() {
        expect(localise("Argentina")).toBe("America");
      });

    it("should localise European countries", function() {
        expect(localise("Portugal")).toBe("Europe");
      });

    it("should localise African countries", function() {
        expect(localise("Nigeria")).toBe("Africa");
      });        
});

What if I want to ignore some tests?

Just use the xdescribe and xit functions instead... Yes, you just have to add an 'x' at the front.

What about set up and tear down?

This is of course a function you must call:

  1. set up: beforeEach()

    beforeEach(function() { //Things you want to do before running each test. });

  2. tear down: afterEach()

    afterEach(function() { //Things you want to do after running each test. });

And this is more or less the structure of a typical Jasmine test.

describe("component", function() {

  beforeEach(function() {
    //Things you want to do before running each test.
    }); 

  afterEach(function() {
    //Things you want to do after running each test.
  });        

  it("should have this behaviour", function() {
      //expects..
  });
});

What am I not covering?

I am not covering the expect function features in extent, Spies, Asynchronous support, etc. That will find in more detail in the Jasmine website.

Download sample from Bitbucket

https://ricardoy1@bitbucket.org/ricardoy1/easy-jasmine.git

About Author

Ricardo Sebastian Youssef

Ricardo Sebastian Youssef