Introduction
In this part of the series, I will discuss the basics of API testing.
The practice code is available in my GitHub Repository.
Testing API is a very inconvenient process to overcome this Node.js introduce a library called supertest. Developers can use SuperTest as a standalone library or with JavaScript testing frameworks like Jest or Mocha.
Configuring SuperTest in Project
Install SuperTest as an npm module and save it to your package.json file as a development dependency:
npm install supertest --save-dev
After installation you can now reference supertest by simply calling require('supertest');
Before getting started supertest I want to share the directory structure which will help you in visualization and understand the code provided in this part of the series.
Getting Started With SuperTest
To test APIs first you must have to create them, then further apply the different tests on APIs.
I have created the APIs in index.js
file as shown below:
index.js
const express = require("express")
const app = express();
app.use(express.json());
const users = []
app.get('/users',(req,res)=>{
res.send(users);
})
app.post('/create-user',(req,res)=>{
const {name,password} = req.body;
users.push(req.body)
res.send(req.body);
})
module.exports = app;
Note: as you may have noticed that I have to write everything in a single file and also used an array instead of a database. I did these all just because of making code simple and understandable.
To test whether the created APIs working properly or not I have used thunder client (vs code extension). first, I have to post the same data using post API then I have got the posted data as shown in the below snap:
Now after creating API, write the test cases based on your desire.
Note: be careful to separate the app.liste()
from index.js
, as every time we run the test the index.js
file will be called because we have imported app module from index.js
into api.test.js
So the best approach is to export app module from index.js
and import it into another file in my case I used the server.js
and there I used the app.listen()
.
api.test.js
const request = require('supertest');
const app = require("../index");
describe('GET APIs', () => {
test('It get all users', async () => {
let result = await request(app).get('/users')
expect(result.body.length).toBeGreaterThanOrEqual(0)
expect(result.statusCode).toBe(200)
})
})
describe('POST APIs', () => {
test('It should create user', async () => {
let result = await request(app).post('/create-user')
.send({
username: "Abid",
password: "1234"
})
expect(result.body).toEqual({
username: "Abid",
password: "1234"
})
expect(result.statusCode).toBe(200);
})
})
After writing the test cases run the test by following command
npm test // this command will test all test-cases in all test-suites
The test result will be as follows:
PASS __test__/sum.test.js
PASS __test__/index.test.js
PASS __test__/api.test.js
Test Suites: 3 passed, 3 total
Tests: 1 skipped, 4 passed, 5 total
Snapshots: 0 total
Time: 3.565 s
These are the basics of API testing, further do it by your own for delete and update.
Conclusion
Using the describe function to combine all GET APIs in a single group will improve the readability. Do it for all other HTTP methods.
Exploring new concepts and sharing them with others is my passion and it gives me pleasure to help and inspire people. If you have any questions, feel free to reach out!
Connect me on Twitter, Linkedin and GitHub
As you know that everyone has a room to improve, so your suggestion is appreciated. And I would love (❤️) to know something new.
Top comments (0)