Test your express server using jest and supertest

Test your express server using jest and supertest

·

1 min read

First of all, it's really easy

Step1: Install jest and supertest

npm install jest supertest

Step2: Add this to your package.json inside scripts

"test": "jest "

Step3: Create a folder named test and create file server.test.js

const request = require('supertest');
const app = require('../index.js');

describe('Test name', () => {

it('test add book', async () => {
const res = await request(app)
.post('/addbook')
.send({

book: "Norwegian Wood",
author: "Haruki Muramaki"

});

expect(res.statusCode).toEqual(200);
expect(res.body.status).toEqual('ADDED');

});

})

Step4: Run the test and you are done

npm test

You are done enjoy testing your api’s.

Cheers!.