JavaScript API Automation Testing — which libraries/frameworks to use?

To do API automation testing with JavaScript, we’ll first need to know which libraries or frameworks we can use that’ll help build our test framework.

Since we are doing API testing, we’ll need some way to make HTTP calls programmatically and for that, we will be
using the SuperTest npm package.


SuperTest is built on top of SuperAgent which basically allows you to make HTTP requests and SuperTest provides a high-level abstraction for testing those HTTP requests. Let’s take a look at an example:

You can easily add assertions by just chaining these expect commands. We can continue to write tests this way but we need a better way to be able to group tests or run individual tests and for that, we’ll need some kind of test framework.

Mocha JS

Mocha JS, is a really popular JavaScript test framework that runs on node js. You get a lot of features pre-build with the Mocha test framework -

  • You can group your tests
  • Skip your tests
  • Use hooks to set up or tear down your tests
  • It also comes with reporting as well as retry support along with many other useful features

Let’s take a look at basic Mocha JS example -

Now, let’s take a look at how Mocha test would look like with SuperTest -

If you notice, we took the SuperTest request block and put that inside the it block. In the same way, we can create multiple it blocks for various test scenarios.

Now, for any test, we need to verify whether something works or not, and for that, we can use assertions.

Chai JS

Chai is an assertion library for node and browser that can be paired with any testing framework (in our case Mocha JS). Chai provides multiple interfaces which are basically different ways for you to write your assertions — so they have should, expect, and assert. So whichever style you are comfortable with using, you can use that in your tests.

Let’s take a look at some Chai assertion example -

Now once we have added assertion, we can start writing tests with no issues. However, we need some way to see a report of all the tests we are running and see the pass / fail status of each.

So good thing is that Mocha comes with pre-built reporters which we can use that is the spec reporter which gives this simple terminal results view.

It’s sufficient enough when we are getting started but we can also implement a nice HTML reporter using mochawesome reporter.


Mochawesome has a nice, modern look and comes with some good features -

  • supports for test and suite nesting
  • shows before and after hooks
  • shows code along with each test which is great for debugging purposes.

Here’s how a sample Mochawesome report looks like -

So there you go using SuperTest, Mocha, Chai, and Mochawesome we can create an API test automation framework in JavaScript.
Note: There are many other alternatives that can be used for these libraries or frameworks, however, for this tutorial series this is what we will be using.

Check out the video below to see a detailed explanation of the above post:

Follow @automationbro on Twitter for the latest updates
Subscribe to my YouTube channel to see more content like this

SDET Architect | YouTuber | Tech Blogger | Love to explore new tools and technologies. Check out my YouTube channel —

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store