Quick and Easy API Testing with NodeJS

Continuous delivery is difficult and here at Achievers, we embody all of it. With several deploys a week, it’s important to stay on top of things and ensure that our API is functioning correctly. Several of our applications, as well as third-party clients, rely on our API. Here’s how we developed a test suite for our platform’s API within three days.

Before diving into libraries, let’s talk about JSON Schemas. A JSON Schema is JSON that can be used to validate the structure of other JSON.

For example, let’s say our API returns this JSON:


{
  "id": 123,
  "name": "John Doe",
  "isRightHanded": true
}

A JSON Schema that validates this JSON will look like this:


{  
   "$schema":"http://json-schema.org/draft-04/schema#",
   "type":"object",
   "properties":{  
      "id":{  
         "type":"integer"
      },
      "name":{  
         "type":"string"
      },
      "isRightHanded":{  
         "type":"boolean"
      }
   },
   "required":[  
      "id",
      "name",
      "isRightHanded"
   ]
}

Using valid responses from our API, we can produce JSON Schemas easily using jsonschema.net, a fantastic tool. JSON Schemas are extremely configurable and are able to handle many different data types and even regular expressions. You can find everything about JSON Schemas over at json-schema.org.

JSON Schemas alone aren’t enough to validate our endpoints. We need to perform HTTP methods on an endpoint, validate against a JSON Schema stored within a file, and then report that data in a readable manner. NodeJS seems like the perfect tool. It’s backed by an awesome community of developers, not to mention a plethora of packages, and the development team is already familiar with Javascript.

Now, using three packages from NPM: mocha, jsonschemas, and supertest we can produce the snippet below.


var assert = require('assert');
var supertest = require('supertest').agent('https://www.example.com');
var validate = require('jsonschema').validate;

describe('GET /reponse.json', function() {
    describe('Endpoint', function() {
        it('Should return with the expected body.', function(done) {
            supertest
                .get('/response.json')
                .expect(function(res) {
                    // Read the JSON Schema from the same directory as test.js
                    var schema = JSON.parse(fs.readFileSync('schema.json'));
                    // In addition we can log error information, but I'll leave that as an exercise ;)
                    assert.equals(validate(res.body, schema).errors.length, 0);
                })
                .expect(200, done);
        })
    })
})

You can then run the tests with mocha: $ mocha test.js

Of course, this is a small snippet of what’s possible.

Combined with Jenkins and a nifty Pipeline Plugin, we’ve now produced a powerful safety net for our production code. We can be confident that our apps receive data in an expected structure with little effort.

CD is a fight that requires constant vigilance. With many small tools like this, we can sanity check our endpoints to ensure nothing has gone awry. Of course, even with API tests, you shouldn’t ignore the rest of the testing pyramid, but this will get you moving in the right direction.

Profile image of author: Sanjay Thakur
Sanjay is a Senior Software Engineer with experience in iOS development, mobile applications, scalability, and web services. He also brings skills in PHP, MySql, HTML, XCode, JavaScript, Swift, and NodeJS to the table. At Achievers, he contributed to developing our platform’s API testing within three days using NodeJS.

STAY ENGAGED

Join our email alert list so you never miss a post.

FOLLOW US