I have a Node.js service which calls an external API to gather some data before returning a response to the user. I want to mock this external API so I can test my service, but unfortunately the response I get back has no content (only a status of 200). Here is my test:
const nock = require('nock');
const expect = require('chai').expect;
const request = require('supertest');
const httpStatus = require('http-status');
var app = require('../index');
describe('Get Account Summary test', function () {
it('Should return a clients account summary successfully', function (done) {
nock('https://external.api')
.post('/v3/restful-api-endpoint')
.reply(200, {
"status": 200,
"message": "This is a mocked response"
});
request(app)
.get('/api/account/summary/dummy-request-id')
.expect(httpStatus.OK)
.end(function(err, res) {
expect(res.body.message).to.equal('This is a mocked response');
done();
});
});
});
res.body
returns undefined. I don't get why.
Without the source of your endpoint, it's difficult to say, but the problem is likely to lie there. There doesn't appear to be much wrong with the test itself. Be sure that you wait for your response from the external API (using promises, async
/await
, or a callback since that's often a source of confusion.