title: Spying on a function used as a constructutor
body: <p>In one of my unit tests I need to spy on a function which is used as a constructor by another function with <code>Sinon</code> library. As per their documentation</p>
  
  <blockquote>
    <p>...<code>sinon.spy(object, "method")</code> creates a spy that wraps the existing function object.method. The spy will behave exactly like the original method (including when used as a constructor)... 
    But so far I have failed to make it work even when trying to spy on a constructor called within the test function let alone called by another function. </p>
  </blockquote>
  
  <p>Unit test:</p>
  
  <pre><code>   it('constructor was called.', () =&gt; {
          const Foo = require('../app/foo');
          const fooModule = module.children.find(m =&gt; m.id.includes('foo.js'));
          const fooSpy = sinon.spy(fooModule, 'exports');
          const f = new Foo(5);
          expect(fooSpy).calledOnce;
      }); 
  </code></pre>
  
  <p>Function to be instantiated:</p>
  
  <pre><code>const Foo = function(param) {
      console.log('Foo called with: ' + param);
  };
  
  Foo.prototype.bar = function(x) {
      console.log(`Foo.prototype.bar() called with x: ` + x);
  };
  
  module.exports = Foo;
  </code></pre>
  
  <p>When I step with debugger I can see the function at <code>const fooSpy = sinon.spy(fooModule, 'exports');</code> being replaced by the spy (has all all <code>sinon</code> properties added like <code>calledOnce</code> and so on...) however when on <code>new Foo(5);</code> it appears that <code>Foo</code> is a non spy object. </p>
  
  <p>I thought this might be a scoping or reference error but I can't seem to find where else <code>Foo</code> would be defined apart from within <code>module.children</code>. It is not on <code>global</code> neither its on <code>window</code> since its running on <code>node</code>.</p>
  
  <p>Currently the test of course fails with:</p>
  
  <pre><code>Foo called with: 5
  
  AssertionError: expected exports to have been called exactly once, but it was called 0 times
      at Context.it (test/fooTest.js:18:23)
  </code></pre>
  
  <p>Thanks in advance for any help!</p>