Skip to content Skip to sidebar Skip to footer

Sinon Spy Function Called But Not Tracked

I am using Mocha and sinon to spy on a function call. The function is called correctly but the spy is not tracking it. Here is the module i am testing export default (() => {

Solution 1:

When you call sinon.spy(Common, 'test1'); you are modifying the test1 field on the Common object to replace it with a spy. However, the code you have in common.js is calling test1directly instead of calling test1through the object that your module exports. Therefore, when you do Common.callThis(), the spy is not touched, and you get the error you observed.

Here's a modified common.js file that would allow your test to pass:

exportdefault (() => {

  var obj = {};

  obj.test1 = functiontest1(){
      console.log('called second func');
      return5;
  }

  obj.callThis = functioncallThis(){
      console.log('called first func');
      // This calls `test1` through `obj` instead of calling it directly.
      obj.test1();
  }

  return obj;
})();

Post a Comment for "Sinon Spy Function Called But Not Tracked"