Reputation: 1522
I need to create a unit test for an endpoint that will make a HTTP request to a certain API and send back the response with the result of HTTP Request,
I use request-promise link is here Node Package and you can see the code below:
router.get("/url", function(req, res){
let options = { url: "http//some-api-en-point",
method: "GET",
resolveWithFullResponse: true
};
rp(options)
.then(function(response) {
if(response.statusCode == 200){
res.send({"data":response.data})
} else {
res.status(404).json("data":"NOT FOUND");
}
})
.catch(err => () => {
res.send(err);
})
});
Expected body (response.data) from the http//some-api-en-point
is:
{
"id": "3f3e2b23e96c5250441d4be2340010ed",
"email": "[email protected]",
"status": "1"
}
I use Mocha, Chai and Sinon to run the the unit tests and you can see the Unit Test case below for above function:
describe('TEST: /URL', () => {
it('it should return Status 200', (done) => {
chai.request(app)
.get('/url')
.end((err, res) => {
sinon.stub(rp, 'Request').resolves({statusCode:200});
expect(res).to.have.status(200);
done();
});
});
});
When I run the npm test
this integration test always fails and need to figure out how to stub this properly.
Upvotes: 2
Views: 4840
Reputation: 102207
Here is the integration test:
server.ts
:
import express from 'express';
import { Router } from 'express';
import rp from 'request-promise';
const app = express();
const router = Router();
router.get('/url', function(req, res) {
let options = { url: 'http//some-api-en-point', method: 'GET', resolveWithFullResponse: true };
rp(options)
.then(function(response) {
if (response.statusCode == 200) {
res.send({ data: response.data });
} else {
res.status(404).json({ data: 'NOT FOUND' });
}
})
.catch(err => {
res.send(err);
});
});
app.use(router);
export { app };
server.spec.ts
:
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
chai.use(chaiHttp);
const mData = {
id: '3f3e2b23e96c5250441d4be2340010ed',
email: '[email protected]',
status: '1'
};
describe('Test: /URL', () => {
it('should return Status 200', done => {
const mResponse = { statusCode: 200, data: mData };
const mRp = sinon.stub().resolves(mResponse);
const { app } = proxyquire('./server', {
'request-promise': mRp
});
chai
.request(app)
.get('/url')
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(mRp.calledWith({ url: 'http//some-api-en-point', method: 'GET', resolveWithFullResponse: true }));
done();
});
});
it('should return status 404', done => {
const mResponse = { statusCode: 500, data: mData };
const mRp = sinon.stub().resolves(mResponse);
const { app } = proxyquire('./server', {
'request-promise': mRp
});
chai
.request(app)
.get('/url')
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(404);
expect(res.body).to.deep.equal({ data: 'NOT FOUND' });
expect(mRp.calledWith({ url: 'http//some-api-en-point', method: 'GET', resolveWithFullResponse: true }));
done();
});
});
it('should send error', done => {
const mError = 'network error';
const mRp = sinon.stub().rejects(mError);
const { app } = proxyquire('./server', {
'request-promise': mRp
});
chai
.request(app)
.get('/url')
.end((err, res) => {
expect(err).to.be.null;
expect(res.body).to.deep.equal({ name: 'network error' });
expect(res).to.have.status(200);
expect(mRp.calledWith({ url: 'http//some-api-en-point', method: 'GET', resolveWithFullResponse: true }));
done();
});
});
});
Unit test result with 100% coverage:
Test: /URL
✓ should return Status 200 (1558ms)
✓ should return status 404 (116ms)
✓ should send error (90ms)
3 passing (2s)
----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
server.spec.ts | 100 | 100 | 100 | 100 | |
server.ts | 100 | 100 | 100 | 100 | |
----------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/57176000
Upvotes: 2