Back to blog
Performance and growth

Quick and Easy API Testing with NodeJS

Get engagement insights delivered to your inbox

Email address

Thank you! A team member will reach out shortly.

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:


Using valid responses from our API, we can produce JSON Schemas easily using, 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 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('');
var validate = require('jsonschema').validate;

describe('GET /response.json', function() {
    describe('Endpoint', function() {
        it('Should return with the expected body.', function(done) {
                .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

Written by

Interested in learning more about Achievers?


We use cookies to help us understand how you use our site so we can show you personalized content and enhance your browsing experience.

Learn more by viewing our Privacy Policy