SDET Unicorns

Write your first API Test using JavaScript

Table of Content

In this post, we’ll take a look at what tools/technologies do we need for writing API tests using JavaScript and then we’ll also write our first API test. So let’s get started…

⚙️ Dependencies:

First off, we’ll need to get the following dependencies installed to set up our base framework –

Note: the above libraries/frameworks are optional to use, you can replace any one or all of them to meet your desired goals.

Setup your project:

You can watch the installation video below to see how to install all these packages and get your project setup.


✍️ Write API Test:

Once you have your project setup, we will begin to write our API test in the users.js file (created as part of the installation video above).

import supertest from 'supertest';
const request = supertest('https://gorest.co.in/public-api/');

import { expect } from 'chai';

// watch the installation video to create your token
const TOKEN = { your_token_here };

describe('Users', () => {
  it('GET /users', (done) => {
    // make a GET call to the users api
    request.get(`users?access-token=${TOKEN}`).end((err, res) => {
      // assertion to ensure data is not empty
      expect(res.body.data).to.not.be.empty;
      // done callback to handle async calls
      done();
    });
  });
});

?‍♂️ Run your test:

Now, its time to run your test, you can do that by running the mocha command or doing npm test which will also run the same mocha command if you followed the installation video.

API test run

There you go, we just created our first API test and it ran successfully ?.

Time to celebrate – 

Celebration Gif


Check out this video to see a detailed explanation on how to write your first API test:

You can also clone the GitHub repo to access this code


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

JavaScript API Automation 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

5/5

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

image

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 »
How to inspect elements in Chrome?

How to Inspect Elements in Chrome?

Understanding how to use the “Inspect Element” feature in Chrome is a critical skill for web developers and designers alike. This tool is indispensable, no matter what web development task you’re tackling.

From resolving layout issues and debugging JavaScript to analyzing a webpage’s structure, Chrome’s Developer Tools’ “Elements” panel offers a robust suite of functionalities that enable you to delve into the HTML, CSS, and JavaScript that power websites.

Read More »