Integration API Test - makingsensetraining/mean-seed-blog GitHub Wiki
Integration API Test
- It's like an E2E test, but we test only the Backend API, without frontend or a client.
We are going to use mochaJS, superTestJS and well the assertion library: shouldJS.
Example:
var supertest = require("supertest");
var should = require("should");
var server = supertest.agent("http://localhost:3001");
describe("Integration API Blog test",function(){
var baseAPIPath = '/api/';
it("should return welcome message on Blog API",function(done){
server
.get(baseAPIPath + 'blog')
.expect("Content-type",/json/)
.expect(200) // THis is HTTP response
.end(function(err,res){
// HTTP status should be 200
res.status.should.equal(200);
// Welcome message:
res.body.should.eql({
message: 'Welcome'
});
done();
});
});
}
So, in the above example we declare first the required variables and create a basic test case.
In that test API we have an endpoint to: GET @/api/blog that returns a json object with a welcome message.
So in the test, we make the call with a get to that path, and expect that the status code have to be equal to 200 and also the body should contain the welcome message.
If we need to test another methods, you could use postand use the .send method to send the request body. Or you may use the delete and put.