Home » Node Js » Node REST API Testing with Jasmine

Node REST API Testing with Jasmine

In last article REST using Node/Express & Mongoose , I talk about setting REST APIs in NodeJS, Express & Mongoose. Today I will discuss unit testing for REST APIs using Jasmine.

Jasmine is behavioral testing framework for javascript applications. It provide easy to write & understand testing rules. While it can be downloaded from https://github.com/jasmine/jasmine. In node js we would use npm package named jasmine-node.

$ npm install --save jasmine-node

We also install request package for easy http calls.

$ npm install --save request

Now create a test folder let we name it spec. We can define our test cases in this folder. Let create a file names hello.spec.js and Put following code into it

describe("Hello World Suite", function() {
    it("check if text is 'Hello World'", function(done) {
        let text = "Hello World";
        expect(text).toBe("Hello World");
   it("check if another text is 'Hello World'", function(done) {
        let text = "Not Hello World";
        expect(text).toBe("Hello World");

Now we run command

$ node_modules/.bin/jasmine-node spec

This command generate assert failures for second test like below


1) Hello World Suite check if another text is 'Hello World'
 Expected 'Not Hello World' to be 'Hello World'.

2 tests, 2 assertions, 1 failure, 0 skipped

If we change text in second test from “Not Hello World” to “Hello World”  and re run command. We will get assert success like below .

2 tests, 2 assertions, 0 failures, 0 skipped

In above test “describe” work as Test Suite, So it can have many related tests, Let example for particular form field data validations, So all validation like not-empty, data type, duplicate can be part of single suite. describe have definition which show use of suite

individual tests inside described via “it”, which have suite description as first argument and callback as second.

We need to statement for compile a test. First a expect statement which is test assertion and other done function which notice the framework to completion of test. We can do many types of assertions

assert().toBeDefined() AND assert().toBeUndefined()
assert().toBeFalsy() AND assert().toBeTruthy()

AND More....

Let we write test for previously REST APIs. Create a file called users.spec.js in spec folder.

var request = require("request");

var base_url = "http://localhost:3000/users"

let User = {

describe("Users List API Exists", function() {
     describe("GET /users", function() {
         it("returns status code 200", function(done) {
             request.get(base_url, function(error, response, body) {

Above test check whether List API is available, It verify this by checking response status code is equal to 200 or not. Add another test to inner describe

it("API Response should be valid json", function(done) {
    request.get(base_url, function(error, response, body) {
        expect(() => {

Above test check if response is valid json or not. When invalid json as response return the native JSON parse function throw error which handled by expect ” not -> toThrow”. So assertion failed while in case of valid json assertion passed.

it("API Response should be valid array of user objects", function(done) {
     request.get(base_url, function(error, response, body) {
        let users = JSON.parse(body);
        const userRows = users.map((userRow) => {
            expect(JSON.stringify(Object.keys(User).sort()) ===     JSON.stringify(Object.keys(userRow).sort())).toBeTruthy();

Above test goes further more and check return fields is valid. This test can be ignored in case of no restriction for fields returned in response.

I will discuss more on jasmine on API testing on further articles.

All code is hosted on Github


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s