1.

Explain the concept of stub in Node.js?

Answer»

Stubs are used in writing tests which are an important part of development. It replaces the whole function which is getting tested.  

This helps in scenarios where we need to test:

  • External calls which make tests slow and difficult to write (e.g HTTP calls/ DB calls)
  • Triggering different outcomes for a piece of code (e.g. what happens if an error is thrown/ if it passes)

For example, this is the function:

const request = require('request');const getPhotosByAlbumId = (id) => {const REQUESTURL = `https://jsonplaceholder.typicode.com/albums/${id}/photos?_limit=3`;return new Promise((resolve, reject) => { request.get(requestUrl, (ERR, res, body) => { if (err) { return reject(err); } resolve(JSON.parse(body)); });});};module.exports = getPhotosByAlbumId;To test this function this is the stubconst expect = require('chai').expect;const request = require('request');const sinon = require('sinon');const getPhotosByAlbumId = require('./index');describe('with Stub: getPhotosByAlbumId', () => {before(() => { sinon.stub(request, 'get') .yields(null, null, JSON.stringify([ { "albumId": 1, "id": 1, "title": "A real photo 1", "URL": "https://via.placeholder.com/600/92c952", "thumbnailUrl": "https://via.placeholder.com/150/92c952" }, { "albumId": 1, "id": 2, "title": "A real photo 2", "url": "https://via.placeholder.com/600/771796", "thumbnailUrl": "https://via.placeholder.com/150/771796" }, { "albumId": 1, "id": 3, "title": "A real photo 3", "url": "https://via.placeholder.com/600/24f355", "thumbnailUrl": "https://via.placeholder.com/150/24f355" } ]));});after(() => { request.get.restore();});it('should getPhotosByAlbumId', (done) => { getPhotosByAlbumId(1).then((photos) => { expect(photos.length).to.equal(3); photos.forEach(photo => { expect(photo).to.have.property('id'); expect(photo).to.have.property('title'); expect(photo).to.have.property('url'); }); done(); });});});


Discussion

No Comment Found