Is it better to unit test using a mock library like 'nock' (nodejs) or to just test the server's http requests directly?
Here is an example of my Express server test for testing if my server is up and testing for yelp data being served from my server.
test.js:
describe('Express server', () => {
let request;
beforeEach(() => {
request = chai.request(app);
});
it('It runs', (done) => {
request.get('/').end((err, res) => {
expect(res).to.have.status(200);
done();
});
})
it('/post serves yelp data', (done) => {
// let yelp = nock('localhost:8080').post('/post', mockplace)
let mockPlace = [
{
name: 'Le Thai',
coords: {
lat: 36.168743,
lng: -115.139866
}
}
];
let expected = {
name: 'Le Thai',
img: 'https://s3-media1.fl.yelpcdn.com/bphoto/vYnAqILo37UXrNvz_5QX0Q/o.jpg',
hours: false,
revcount: 1425,
rating: 4,
price: '$$',
location: '523 Fremont St,Las Vegas, NV 89101',
phone: '(702) 778-0888',
url: 'https://www.yelp.com/biz/le-thai-las-vegas?adjust_creative=euqH0_vzVDHpkWNkOrRvRg&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=euqH0_vzVDHpkWNkOrRvRg'
}
request.post('/post').set('content-type', 'application/json').send(mockPlace).end((err, res) => {
assert.deepEqual(res.body[0], expected);
done();
})
})
});
I've tried testing in both ways described above, and have noticed that using a mock service like nock dramatically decreases the test suite run time. Would this be the only reason to use a mock service?
If I'm using a mock service, then what is the point of doing the testing in the first place?