Gourab Paul
Gourab Paul

Reputation: 655

Sinon stub is not changing the behavior if dependency is already loaded

process.js

const { getConnection, closeConnection } = require('./utils/db-connection.js');

const getQueryCount = query => new Promise((resolve, rejects) => {
  getConnection().then((conn) => {
    conn.query(query, (err, count) => {
      closeConnection(conn);
      if (err) {
        log.info(`Get Count Error:  ${err}`);
        return rejects(err);
      }
      return resolve(count[0][1]);
    });
  });
});

process.test.js

const dbConn = require('../src/utils/db-connection.js');
describe('all count', () => {
    beforeEach(() => {
      sinon.stub(dbConn, 'getConnection').resolves({ query: sinon.stub().yields(null, [[0, 5]]) });
      sinon.stub(dbConn, 'closeConnection');
  const process = require('./src/process'); //module is loaded after dependencies are stubbed

    });

    it('getQueryCount', () => {
      process.getQueryCount('query').then((result) => {
        expect(result).to.equal(5);
      });
    });
  });

In the above scenario stub works but throws Module did not self-register. And below scenario stub doesn't work.

process.test.js

  const dbConn = require('../src/utils/db-connection.js');
      const process = require('./src/process'); // test file is loaded at the starting
    describe('all count', () => {
        beforeEach(() => {
          sinon.stub(dbConn, 'getConnection').resolves({ query: sinon.stub().yields(null, [[0, 5]]) });
          sinon.stub(dbConn, 'closeConnection');
    
        });
    
        it('getQueryCount', () => {
          process.getQueryCount('query').then((result) => {
            expect(result).to.equal(5);
          });
        });
      });

"sinon": "9.0.2" "mocha": "^3.4.2", //even checked with mocha 7.0.1 node --version v8.11.3 npm --v 6.2.0

Upvotes: 0

Views: 1034

Answers (1)

andreyunugro
andreyunugro

Reputation: 1116

In order stub to work, you need to change the implementation on process.js, from using destructuring assignment to normal assignment (reference).

const { getConnection, closeConnection } = require('./utils/db-connection.js');

for example to

const db = require('./utils/db-connection.js');

Complete example:

// Simplified process.js
const db = require('./db-connection.js');

const getQueryCount = (query) => new Promise((resolve, rejects) => {
  // Called using db.getConnection.
  db.getConnection().then((conn) => {
    conn.query(query, (err, count) => {
      // Called using db.closeConnection.
      db.closeConnection(conn);
      if (err) {
        return rejects(err);
      }
      return resolve(count[0][1]);
    });
  });
});

module.exports = { getQueryCount };
const sinon = require('sinon');
const { expect } = require('chai');

// Simple path just for example.
const dbConn = require('./db-connection.js');
const process = require('./process.js');

describe('all count', function () {
  let stubGetConnection;
  let stubCloseConnection;

  beforeEach(function () {
    stubGetConnection = sinon.stub(dbConn, 'getConnection').resolves({ query: sinon.stub().yields(null, [[0, 5]]) });
    stubCloseConnection = sinon.stub(dbConn, 'closeConnection');
  });

  it('getQueryCount', function (done) {
    process.getQueryCount('query').then((result) => {
      expect(result).to.equal(5);
      // Need to verify whether stub get called once.
      expect(stubGetConnection.calledOnce).to.equal(true);
      expect(stubCloseConnection.calledOnce).to.equal(true);
      // Need to notify mocha when it is done.
      done();
    });
  });
});

Note: I use done there because it is an asynchronous code.

$ npx mocha process.test.js --exit


  all count
    ✓ getQueryCount


  1 passing (17ms)

$

Upvotes: 6

Related Questions