Skip to content

Introduction to unit tests with Jasmine node

Gabriel R. Sezefredo edited this page Jun 25, 2018 · 1 revision

Jasmine is a testing framework that describes itself as "behavior driven", but is often used for unit testing. It was first intended to test front-end javascript, but was adapted to run tests on the back-end too, it's one of the most used testing frameworks for node.js nowadays

The following example demonstrates how we can use Jasmine to develop unit tests in Javascript.

The code we are going to test is the following:

ObjectA.js

function ObjectA(param) {
    if (param !== undefined) {
        this.status = true;
    }else {
        this.status = false;
    }
}

module.exports = ObjectA;

This file exports a single object with a constructor that may receive a parameter or not, and sets an attribute accordingly.

In order to start testing it, we will first need to download jasmine:

npm install jasmine -g

After downloading jasmine, we have to initialize it in the project, in order to do that, run the following in the root directory of the application:

jasmine init

You will notice a spec folder was created, this is the default folder which jasmine uses to store tests and configs, inside it, a file jasmine.json was created inside a support folder, that set-ups some basic configurations for jasmine.

In order to write our first tests, we must create a file, inside the spec folder create a file called ObjectASpec.js, that will be used to write tests for ObjectA.

We will first start the file by importing the module we want to test and jasmine itself:

const jasmine = require('jasmine');
const ObjectA = require('./ObjectA');

In order to run specs we must first define a suite, the way jasmine handles this is by using the describe and it functions. The describe function wraps a suite, whereas it wraps a spec, a suite can contain many specs, and we normally write a test suite per file. A test suite should contain related tests, defining a test suite for each object is a good approach most of the times.

Since our ObjectA is a really simple object, there's not much to test in it, we will want to make sure that it's status is updated accordingly to the parameters we send to it, so, in order to do that, we will start writing a suite and 2 specs as follows:

describe('ObjectA', () => {
    it('should have a positive status when instantiated with a param', () => {
        // TODO: Test hypothesis
    });

    it('should have a negative status when instantiated without a param', () => {
        // TODO: Test hypothesis
    });
});

As you can see, a test suite (describe) receives a brief description as well as the specs (it). These descriptions are meant to help us read test reports in case of failure and to explain what the test suite is trying to achieve.

Inside each it block is where our tests are going to sit, let's implement them and then explain what is happening:

describe('ObjectA', () => {
    it('should have a positive status when instantiated with a param', () => {
        let obj = new ObjectA("hi");
        expect(obj.status).toBe(true);
    });

    it('should have a negative status when instantiated without a param', () => {
        let obj = new ObjectA();
        expect(obj.status).toBe(false);
    });
});

Pretty straightforward, we instantiate the object with a parameter in the first spec, and expects its status to be true (following our intended behavior). The same goes to the second spec, when no parameter is used on instantiation. Notice how we used a function expect and toBe to assert what we wanted, these are standard jasmine assertion functions and there are many more to explore.

Our finished ObjectASpec.js is then:

const jasmine = require('jasmine');
const ObjectA = require('../ObjectA');

describe('ObjectA', () => {
    it('should have a positive status when instantiated with a param', () => {
        let obj = new ObjectA("hi");
        expect(obj.status).toBe(true);
    });

    it('should have a negative status when instantiated without a param', () => {
        let obj = new ObjectA();
        expect(obj.status).toBe(false);
    });
});

The last step now is to run our tests, in order to do that, we only have to run jasmine in our project folder, it will automatically run the tests defined in the spec folder and report the results:


> jasmine
Randomized with seed 80774
Started
..


2 specs, 0 failures
Finished in 0.006 seconds
Randomized with seed 80774 (jasmine --random=true --seed=80774)

Clone this wiki locally