Automation API Testing in Javascript using Mocha, Chai, and Supertest (A Beginner’s Guide)

Sofia Awiliyah
4 min readAug 19, 2023

API testing is one of the main QA jobs. It helps us test the response before it’s combined with the design from the front-end Engineer. We usually use Postman to test APIs manually. At the advanced level, we can use Newman to get better reports. But, it still has drawbacks, we cannot run positive and negative scenarios simultaneously.

To overcome this problem, we can create an API test script using Javascript. It provides so many libraries to create and automate API testing. In this section, we will use the frameworks Mocha, Chai, and Supertest. Here’s some explanation about each framework:

  • Supertest: Supertest provides a high-level abstraction for testing HTTP, while still allowing users to drop down to the lower-level API provided by superagent.
  • Mocha : Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.
  • Chai : Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any Javascript testing framework.

Prerequisite

Make sure you have already installed Node.js and npm on your local computer. If you haven’t installed it, you can install it first
through the link: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm

Setup Project

After successfully installing, check the installation using

$ node -version

$ npm -version

Then, create a new folder with the command:

$ mkdir api-automation

$ cd api-automation

Initialize the project with the default setting

$ npm init

It will generate a package.json file. Then install the package using:

$ npm install

Here’s the folder structure after installation.

Getting Started

For this tutorial, we will use APIs from https://reqres.in/ to log in. We have two scenarios for positive and negative tests.

Login Successful
Login Unsuccessful

1. Installing package

First, you have to install the libraries using the command below

$ npm install supertest

$ npm install mocha

$ npm install chai

Then, check the dependencies on the package.json file

2. Create a new file called “login.js”

Import the libraries Supertest and Chai with the scripts below :

const request = require("supertest");
const { expect } = require("chai");

3. Create a function to store the endpoint and payload

async function login(payload) {
const response = await request("https://reqres.in")
.post("/api/login")
.send(payload)
return response
}

4. Create test scenarios and test cases.

Add your scenario name to the “describe” function and your test case name to the “it” function. Here’s the example of test scenario and test cases

describe("Login Feature", () => {
it("Success Login", async () => {
const payload = {
"email": "eve.holt@reqres.in",
"password": "cityslicka"
}
const response = await login(payload)

//ASSERTION
expect((await response).status).to.equal(200);
})

it("Failed Login", async () => {
const payload = {
"email": "peter@klaven"
}
const response = await login(payload)

//ASSERTION
expect((await response).status).to.equal(400);
})

})

5. Run the Program

Run the program with the command below:

$ npx mocha login.js

If the test result passed, you will see the screen below

Or you can also add the command to the package.json > scripts like below:

Run the program with

$ npm run test

Here’s the full code API test scenario to log in to reqres.in.

const request = require("supertest");
const { expect } = require("chai");

async function login(payload) {
const response = await request("https://reqres.in")
.post("/api/login")
.send(payload)
return response
}

describe("Login Feature", () => {
it("Success Login", async () => {
const payload = {
"email": "eve.holt@reqres.in",
"password": "cityslicka"
}
const response = await login(payload)

//ASSERTION
expect((await response).status).to.equal(200);
})

it("Failed Login", async () => {
const payload = {
"email": "peter@klaven"
}
const response = await login(payload)

//ASSERTION
expect((await response).status).to.equal(400);
})

})

You can also add more assertions based on your needs. See full documentation about assertions here: https://www.npmjs.com/package/chai

--

--