Write your first API Test using JavaScript

https://youtu.be/TTyl1psI4DE

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:

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:

✍️ Write API Test:

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:

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

Time to celebrate -

Check out this video to see a detailed explanation of 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 -

I hope this post helped you out, let me know in the comments below!

Happy testing! 😄

Subscribe to my YouTube channel
Support my work — https://www.buymeacoffee.com/automationbro
Follow @automationbro on Twitter

SDET Architect | YouTuber | Tech Blogger | Love to explore new tools and technologies. Check out my YouTube channel — https://youtube.com/automationbro