title: How do I stub node.js built-in fs during testing?
body: <p>I want to stub node.js built-ins like <code>fs</code> so that I don't actually make any system level file calls. The only thing I can think to do is to pass in <code>fs</code> and all other built-ins as an argument to all of my functions to avoid the real fs from being used. This seems a little bit silly and creates a verbose function signature crowded with built ins as arguments.</p>
  
  
  
  <pre class="lang-js prettyprint-override"><code>var fs = require('fs');
  
  function findFile(path, callback) {
    _findFile(fs, path, callback);
  }
  
  function _findFile(fs, path, callback) {
    fs.readdir(path, function(err, files) {
       //Do something.
    });
  }
  </code></pre>
  
  <p>And then during testing:</p>
  
  <pre class="lang-js prettyprint-override"><code>var stubFs = {
    readdir: function(path, callback) {
       callback(null, []);
    }
  };
  
  _findFile.(stubFs, testThing, testCallback);
  </code></pre>
  
  <p>There's a better way than this right?</p>