SDET Unicorns

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

Table of Content

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:

const request = require('supertest');

  .get('/user') // access user route
  .expect('Content-Type', /json/) // verify the content type
  .expect('Content-Length', '15')
  .expect(200) // verify the status code
  .end(function (err, res) {
    if (err) throw err; // any additional code

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 –

describe('Array', function () {
  // describe block allows you to group your tests
  describe('#indexOf()', function () {
    it('should return -1 when the value is not present', function () {
      // it block is where you will run your test
      // any verification here...

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

describe('GET /user', function () {
  it('responds with json', function (done) {
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200, done);

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

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 shouldexpect, 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 –

// Using chai.should();'string');

// Using chai.expect

// Using chai.assert
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');

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.

Alt Text

It is 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 – Alt Text

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:

To learn more about API testing, check out my free tutorial series here –

JavaScript API Automation Testing Tutorial Series

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster


Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »

Common Types of Software Bugs

Dealing with bugs is one of the biggest headaches in the software development lifecycle. No matter how carefully someone writes code, it’s impossible to create a software product that works perfectly every time. Skipping detailed testing often causes major issues later on.

Read More »