Testing: Mocha and node differ re fs.exists
<p>First, while I'm an experienced programmer, I'm relatively new to node, and very new to mocha, so my issue may be ME!  In which case, I'll be happy to find that out too!</p>

<p>Some code I'm writing makes use of an npm module which contains a call to fs.exists.  I get different results when I run a test with fs.exists when run via node vs when run vs mocha; in my opinion, I should be receiving the same answer; the file does exists, so the result should be true in both cases, but instead, it's true when invoked via node, and false when invoked via mocha.  Well, it's not functioning correctly.</p>

<p>I'm aware that fs.exists is deprecated, but that's in the npm module I'm using, so unless I modify the module, that's what I'm using.</p>

<p>The difference is associated with how I invoke mocha.  </p>

<p>Per some site recommendations, I edited my package.json file to include, in the 'scripts' section, the following statement: "test": "mocha test", and I have placed my tests in this 'test' directory.  This is invoked via npm test.</p>

<p>After experiencing problems, I also installed did npm install -g mocha.</p>

<p>My test file is testVersion.js</p>

<p>The result is that I get different answers when I invoke one of the following three ways when invoking fs.exists via node, npm test, and mocha testVersion.js; I'm expecting 'true':
 - node testVersion.js returns true - it finds the file.
 - mocha testVersion.js returns true - it finds the file.
 - npm test returns false - it does not find the file.</p>

<p>I'm suspecting that maybe different versions of mocha or node are being invoked on me, but don't have enough experience to be able to determine this on my own.</p>

<p><strong>So my question is: why am I getting different results?</strong></p>

<p>Here's my code for testVersion.js</p>

<pre><code>var expect = require('chai').expect;
var assert = expect.assert;
var fs = require("fs");
var isInTest = typeof global.it === 'function';  //exe via mocha or node

console.log('isInTest mode: ', isInTest);

if(!isInTest) {
  console.log('NOT TEST MODE: invoking fs.exists');
  fs.exists("testVersion.js", function(info) {
    console.log('NOT TEST MODE: fs.exists callback RAN.');
    console.log('NOT TEST MODE: fs.exists: should be true and is: ', info);
  })
}



if(isInTest) {
  describe("Test Mocha and fs.exists issue", function() {
    it("Test that file self exists.", function() {
      console.log('TEST MODE: invoking fs.exists');
      expect(fs.exists("testVersion.js", function(result) {
        console.log('TEST MODE: fs.exists callback RAN.');
        console.log('TEST MODE: fs.exists: should be true, and is: ', result);
        return true;
      })).to.equal(true);
    });
  });
};
</code></pre>