title: nodejs override a function in a module
body: <p>I am trying to test a function in a module. This function ( I will refer to it as function_a ) calls a different function ( function_b ) within the same file. So this module looks like this:</p>
  
  <pre><code>//the module file
  
  module.exports.function_a = function (){ 
    //does stuff
    function_b()
  };
  
  module.exports.function_b = function_b = function () {
    //more stuff
  }
  </code></pre>
  
  <p>I need to test function_a with a specific result from function_b.</p>
  
  <p>I would like to override function_b from my test file, then call function_a from my test file, resulting in function_a calling this override function instead of function_b.</p>
  
  <p><em>Just a note, I have tried and succeeded in overriding functions from separate modules, like <a href="https://stackoverflow.com/questions/17074200/node-js-override-standard-module">this</a> question, but that is not what I am interested in.</em></p>
  
  <p>I have tried the code below, and as far as I know, doesn't work. It <em>does</em> illustrates what I am going for, though.</p>
  
  <pre><code>//test file
  that_module = require("that module")
  that_module.function_b = function () { ...override ... }
  that_module.function_a() //now uses the override function
  </code></pre>
  
  <p>Is there a correct way to do this?</p>