title: mocha/chai return undefined
body: <p>I have a working node script and am now trying to learn mocha/chai to add some unit tests.</p>
  
  <pre><code>//Module Code
  var path = require('path');
  var recursive = require('recursive-readdir');
  
  
  function findData(folderPath) {
    recursive(folderPath, function (err, files) {
      return files;
    });
  };
  
  module.exports.findData = findData;
  </code></pre>
  
  <p>My mocha test code:</p>
  
  <pre><code>var expect = require('chai').expect;
  
  describe('checkData', function () {
    var findData = require('../custom_modules/findData').findData;
    var path;
  
    before (function () {
      path = '/Data'
    });
  
    it('should have 53 files in array', function () {
      expect(findData(path)).to.have.lengthOf(53);
    })
  
  });
  </code></pre>
  
  <p>However, it always fails because the return seems to be undefined. So i stripped my module code back to test a <code>return true</code> and that worked.</p>
  
  <p>So it must the the asynchronous nature of the recursive module so I then tried add in callbacks to my module code:</p>
  
  <pre><code>var path = require('path');
  var recursive = require('recursive-readdir');
  
  
  function findData(folderPath, cb) {
    recursive(folderPath, function (err, files) {
      cb(null, files);
    });
  };
  
  module.exports.findData = findData;
  </code></pre>
  
  <p>But that didn't work either.</p>
  
  <p>The weird thing is if I run <code>node index.js</code> i get the list of files.</p>
  
  <p>Can anyone explain to me how this code can work normally but when I try to use mocha/chai to test I get undefined?</p>
  
  <p>Thanks</p>
  
  <p>EDITED:</p>
  
  <p>So based on what @Louis said in the comments I have added a callback to the function.</p>
  
  <pre><code>describe('checkData', function () {
    var findData = require('../custom_modules/findData').findData;
    var path;
    var files;
  
    before (function () {
      path = '/Users/tjmartin/Documents/OSData/OpenNames/Data'
    });
  
    it('should have 53 files in array', function () {
      expect(findData(path, function(results) {
          files = results;
        })).to.have.lengthOf(53);
    })
  });
  </code></pre>
  
  <p>But this still returns an undefined.</p>