Search code examples
javascriptnode.jsasync-awaitsinonsinon-chai

Sinon stubbing giving 'is not a function' error


first time really using sinon and I am having some issues with the mocking library.

All I am trying to do is stub/mock out a function from a dao class called myMethod. Unfortunatly, I am getting the error: myMethod is not a function, which makes me believe I am either putting the await/async keywords in the wrong spots of the test or I don't understand sinon stubbing 100%. Here is the code:

// index.js
async function doWork(sqlDao, task, from, to) {
  ...
  results = await sqlDao.myMethod(from, to);
  ...
}

module.exports = {
  _doWork: doWork,
  TASK_NAME: TASK_NAME
};
// index.test.js

const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");

const { _doWork, TASK_NAME } = require("./index.js");
const SqlDao = require("./sqlDao.js");

.
.
.

  it("given access_request task then return valid results", async () => {
    const sqlDao = new SqlDao(1, 2, 3, 4);
    const stub = sinon
      .stub(sqlDao, "myMethod")
      .withArgs(sinon.match.any, sinon.match.any)
      .resolves([{ x: 1 }, { x: 2 }]);

    const result = await _doWork(stub, TASK_NAME, new Date(), new Date());
    console.log(result);
  });

With error:

  1) doWork
       given task_name task then return valid results:
     TypeError: sqlDao.myMethod is not a function


Solution

  • Your issue is that you're passing stub to _doWork instead of passing sqlDao.

    A stub isn't the object that you just stubbed. It is still a sinon object that you use to define the behaviour of the stubbed method. When you're done with your tests, you use stub to restore stubbed object.

    const theAnswer = {
        give: () => 42
    };
    
    const stub = sinon.stub(theAnswer, 'give').returns('forty two');
    
    // stubbed
    console.log(theAnswer.give());
    
    // restored 
    stub.restore();
    console.log(theAnswer.give());
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/7.2.4/sinon.min.js"></script>