Quick and Easy API Testing with NodeJS

June 23, 2016 Sanjay Thakur

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:

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

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.

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.


Quick and Easy API Testing with NodeJS was originally published in Achievers Tech on Medium, where people are continuing the conversation by highlighting and responding to this story.

Previous Article
The Perks In A Great Development Culture
The Perks In A Great Development Culture

What makes a great development culture? Free drinks? Free breakfast? Everyday happy hour? Sure, those thing...

Next Article
Tools of our trade
Tools of our trade

I often get asked what tools I use when designing and building products. Learning new tools is something I ...