Wonder how to write tests for you API. Well you can use which is can be used to test HTTP endpoints. SuperTest Before starting let’s create a simple API using node + express. In express let’s use the the route given to us by them in order to learn library. This article is not focusing on crating a rest API using express framework. SuperTest SuperTest Let’s go to the /routes/users.js file and create some endpoints. express = require('express'); router = express.Router(); var var _/*** get all users*/_router.get('/', (req, res, next) { res.json('all users sent');}); function return _/*** Get a specific user*/_router.get('/:id', (req, res, next) { (req.params.id === 'U001') { // just to demo res.json("user U001 found");} res.status(404).json('user not found');}); function if return return _/*** Add a user*/_router.post('/', (req, res, next) { content = req.body; (content.id) { //just to demo res.status(201).json("user created");} res.status(400).json('user not created');}); function let if return return module.exports = router; I’ve crated two simple get methods to fetch user data and a post method to save a user. LOL, I was kidding there’s no actual database integration or save functionality just sending some JSON outputs based on some dummy logic 😹 and status codes to test our API. You can create a real working API. But remember to send relevant so it’s easy when writing unit tests later. HTTP status codes Now let’s focus on writing API tests using . First you need to install the dependency as a devDependancy. SuperTest npm install supertest --save-dev This will include mocha as well because super test use mocha test framework. But you don’t need to install it it comes with . supertest Then lets create a directory called in the root level of the project and create a file called This file is the file where we write the API tests. test apiTest.js. First thing we need is the supertest module. //apiTest.jsconst request = require('supertest'); Then we need to pass the http.Server to the request() method of supertest. To do that let’s include our express app as follows. //apiTest.jsconst request = require('supertest');const app = require('../app'); //reference to you app.js file The let’s write the first API test to test the endpoint . If this endpoint works correctly it should return status code 200. That’s how the user route file is implemented . See the following code res.json() will automatically set the status code 200. So no worries. http://localhost:3000/users _//from users.js route file/*** get all users*/_router.get('/', (req, res, next) { res.json('all users sent');}); function return Ok, how we write the test for this route in out apiTest.js using supertest. //apiTest.js request = require('supertest'); app = require('../app'); const const //==================== user API test ==================== _/*** Testing get all user endpoint*/_describe('GET /users', () {it('respond with json containing a list of all users', (done) {request(app).get('/users').set('Accept', 'application/json').expect('Content-Type', /json/).expect(200, done);});}); function function Here method is coming from mocha test framework. We simply specify the endpoint name as the first argument and then the function to write the test case. Here Mocha () is for grouping the test cases while () is used to write the real test cases. We give a simple description of what we are going to test as the first argument in it() function. Then the callback function to be called. describe describe it () needs the HTTP.server so we pass our express app reference. Then in () we specify the route endpoint. Here we are in the app so can omit part. Just give the route endpoint. request get http://localhost:3000/ in () we set the HTTP header attributes. Then in () we check for the return values including Header values and body values. set expect In this example we check whether the returned content type is JSON. Because we send the response as JSON using res.json() method. Then expect(200) is to check the returned status code is equal to 200. Then we end the test by calling our callback function. done Right so far so good. But how can we run this tests. Well you have to use make this change in your file. package.json "scripts": {"start": "node ./bin/www","test": "mocha 'test/apiTest.js'" //path to your test file} Now you can run the test cases by simply using > npm test After running the test file you’ll see this output. > mocha 'test/apiTest.js' GET /usersGET /users 200 5.289 ms - 16✓ respond with json containing a list of all users 1 passing (38ms) Let’s continue writing more test cases of all the endpoints in the users.js file. request = require('supertest'); app = require('../app'); const const //==================== user API test ==================== _/*** Testing get all user endpoint*/_describe('GET /users', () {it('respond with json containing a list of all users', (done) {request(app).get('/users').set('Accept', 'application/json').expect('Content-Type', /json/).expect(200, done);});}); function function _/*** Testing get a user endpoint by giving an existing user*/_describe('GET /user/:id', () {it('respond with json containing a single user', (done) {request(app).get('/users/U001').set('Accept', 'application/json').expect('Content-Type', /json/).expect(200, done);});}); function function _/*** Testing get a user endpoint by giving a non-existing user*/_describe('GET /user/:id', () {it('respond with json user not found', (done) {request(app).get('/users/idisnonexisting').set('Accept', 'application/json').expect('Content-Type', /json/).expect(404) //expecting HTTP status code.expect('"user not found"') // expecting content value.end((err) => { (err) done(err);done();});});}); function function if return _/*** Testing post user endpoint*/_describe('POST /users', () { data = {"id": "1","name": "dummy","contact": "dummy","address": "dummy"}it('respond with 201 created', (done) {request(app).post('/users').send(data).set('Accept', 'application/json').expect('Content-Type', /json/).expect(201).end((err) => { (err) done(err);done();});});}); function let function if return _/*** Testing post user endpoint*/_describe('POST /users', () { data = {//no id"name": "dummy","contact": "dummy","address": "dummy"}it('respond with 400 not created', (done) {request(app).post('/users').send(data).set('Accept', 'application/json').expect('Content-Type', /json/).expect(400).expect('"user not created"').end((err) => { (err) done(err);done();});});}); function let function if return If you look closely at the code in file you’ll get the rhythm. I’ve only showed GET and POST methods but you can try PUT and DELETE methods with real working API. apiTest.js If you run this test file using command finally you’ll see an output like this. npm test > mocha 'test/apiTest.js'GET /usersGET /users 200 5.610 ms - 16✓ respond with json containing a list of all users GET /user/:idGET /users/U001 200 0.870 ms - 17✓ respond with json containing a single user GET /user/:idGET /users/idisnonexisting 404 0.458 ms - 16✓ respond with json user not found POST /usersPOST /users 201 10.196 ms - 14✓ respond with 201 created POST /usersPOST /users 400 0.607 ms - 18✓ respond with 400 not created 5 passing (62ms) Better to refer for more knowledge. https://github.com/visionmedia/supertest
Share Your Thoughts